PageRenderTime 681ms CodeModel.GetById 17ms RepoModel.GetById 7ms app.codeStats 0ms

/qt-everywhere-opensource-src-4.8.2/demos/embedded/desktopservices/contenttab.cpp

#
C++ | 151 lines | 82 code | 19 blank | 50 comment | 4 complexity | 239dda62f41686dbc00de9b84523f649 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-4.0, LGPL-3.0, GPL-2.0, LGPL-2.0, LGPL-2.1, GPL-3.0
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the demonstration applications of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. // EXTERNAL INCLUDES
  42. #include <QKeyEvent>
  43. #include <QMessageBox>
  44. #include <QListWidget>
  45. #include <QVBoxLayout>
  46. #include <QFileInfoList>
  47. #include <QListWidgetItem>
  48. // INTERNAL INCLUDES
  49. // CLASS HEADER
  50. #include "contenttab.h"
  51. // CONSTRUCTORS & DESTRUCTORS
  52. ContentTab::ContentTab(QWidget *parent) :
  53. QListWidget(parent)
  54. {
  55. setDragEnabled(false);
  56. setIconSize(QSize(45, 45));
  57. }
  58. ContentTab::~ContentTab()
  59. {
  60. }
  61. // NEW PUBLIC METHODS
  62. void ContentTab::init(const QDesktopServices::StandardLocation &location,
  63. const QString &filter,
  64. const QString &icon)
  65. {
  66. setContentDir(location);
  67. QStringList filterList;
  68. filterList = filter.split(";");
  69. m_ContentDir.setNameFilters(filterList);
  70. setIcon(icon);
  71. connect(this, SIGNAL(itemClicked(QListWidgetItem*)),
  72. this, SLOT(openItem(QListWidgetItem*)));
  73. populateListWidget();
  74. }
  75. // NEW PROTECTED METHODS
  76. void ContentTab::setContentDir(const QDesktopServices::StandardLocation &location)
  77. {
  78. m_ContentDir.setPath(QDesktopServices::storageLocation(location));
  79. }
  80. void ContentTab::setIcon(const QString &icon)
  81. {
  82. m_Icon = QIcon(icon);
  83. }
  84. void ContentTab::populateListWidget()
  85. {
  86. QFileInfoList fileList = m_ContentDir.entryInfoList(QDir::Files, QDir::Time);
  87. foreach(QFileInfo item, fileList) {
  88. new QListWidgetItem(m_Icon, itemName(item), this);
  89. }
  90. }
  91. QString ContentTab::itemName(const QFileInfo &item)
  92. {
  93. return QString(item.baseName() + "." + item.completeSuffix());
  94. }
  95. QUrl ContentTab::itemUrl(QListWidgetItem *item)
  96. {
  97. return QUrl("file:///" + m_ContentDir.absolutePath() + "/" + item->text());
  98. }
  99. void ContentTab::keyPressEvent(QKeyEvent *event)
  100. {
  101. switch (event->key()) {
  102. case Qt::Key_Select:
  103. openItem(currentItem());
  104. default:
  105. QListWidget::keyPressEvent(event);
  106. break;
  107. }
  108. }
  109. void ContentTab::handleErrorInOpen(QListWidgetItem *item)
  110. {
  111. Q_UNUSED(item);
  112. QMessageBox::warning(this, tr("Operation Failed"), tr("Unknown error!"), QMessageBox::Close);
  113. }
  114. // NEW SLOTS
  115. void ContentTab::openItem(QListWidgetItem *item)
  116. {
  117. #if defined(Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86)
  118. // Opening music files doesn't work in Symbian emulator and in some SDKs freezes the
  119. // emulator entirely, so prevent it.
  120. QStringList nameFilters = m_ContentDir.nameFilters();
  121. if (nameFilters.contains("*.mp3")) {
  122. QMessageBox::warning(this, tr("Operation Failed"), tr("Action not supported in emulator."),
  123. QMessageBox::Close);
  124. } else
  125. #endif
  126. {
  127. bool ret = QDesktopServices::openUrl(itemUrl(item));
  128. if (!ret)
  129. handleErrorInOpen(item);
  130. }
  131. }
  132. // End of File