/src/plugins/parts/filemanagerpart/filesystemtoolwidget.cpp

https://github.com/ABBAPOH/andromeda · C++ · 171 lines · 144 code · 27 blank · 0 comment · 15 complexity · d40827b16e878e7c90bdb002fb291a38 MD5 · raw file

  1. #include "filesystemtoolwidget.h"
  2. #include "filesystemtoolwidget_p.h"
  3. #include <QtGui/QDesktopServices>
  4. #include <QtGui/QKeyEvent>
  5. #if QT_VERSION >= 0x050000
  6. #include <QtWidgets/QHeaderView>
  7. #include <QtWidgets/QMenu>
  8. #include <QtWidgets/QVBoxLayout>
  9. #else
  10. #include <QtGui/QHeaderView>
  11. #include <QtGui/QMenu>
  12. #include <QtGui/QVBoxLayout>
  13. #endif
  14. #include <Parts/OpenStrategy>
  15. #include <FileManager/FileSystemModel>
  16. #include "filesystemtoolmodel.h"
  17. using namespace Parts;
  18. using namespace FileManager;
  19. FileSystemToolWidget::TreeView::TreeView(QWidget * /*parent*/)
  20. {
  21. setEditTriggers(QAbstractItemView::NoEditTriggers);
  22. }
  23. void FileSystemToolWidget::TreeView::keyPressEvent(QKeyEvent *event)
  24. {
  25. QModelIndex index = currentIndex();
  26. switch (event->key()) {
  27. case Qt::Key_Enter:
  28. case Qt::Key_Return:
  29. emit triggered(index);
  30. return;
  31. case Qt::Key_Down:
  32. if (event->modifiers() & Qt::ControlModifier) {
  33. emit triggered(index);
  34. return;
  35. }
  36. default:
  37. break;
  38. }
  39. QTreeView::keyPressEvent(event);
  40. }
  41. FileSystemToolWidget::FileSystemToolWidget(QWidget *parent) :
  42. ToolWidget(*new FileSystemToolModel, parent)
  43. {
  44. FileSystemToolModel *model = static_cast<FileSystemToolModel *>(this->model());
  45. model->setParent(this);
  46. QVBoxLayout *layout = new QVBoxLayout(this);
  47. layout->setContentsMargins(0, 0, 0, 0);
  48. layout->setSpacing(0);
  49. m_view = new TreeView(this);
  50. layout->addWidget(m_view);
  51. m_view->header()->hide();
  52. m_view->setModel(model->fileSystemModel());
  53. m_view->hideColumn(1);
  54. m_view->hideColumn(2);
  55. m_view->hideColumn(3);
  56. m_view->expandAll();
  57. m_view->setDragDropMode(QAbstractItemView::DropOnly);
  58. connect(m_view, SIGNAL(clicked(QModelIndex)),
  59. this, SLOT(onActivated(QModelIndex)));
  60. connect(m_view, SIGNAL(triggered(QModelIndex)),
  61. this, SLOT(onActivated(QModelIndex)));
  62. connect(m_view, SIGNAL(doubleClicked(QModelIndex)),
  63. this, SLOT(open()));
  64. connect(model, SIGNAL(urlChanged(QUrl)), SLOT(onUrlChanged(QUrl)));
  65. }
  66. void FileSystemToolWidget::onActivated(const QModelIndex &index)
  67. {
  68. const FileSystemModel *model = qobject_cast<const FileSystemModel *>(index.model());
  69. QString path = index.data(QFileSystemModel::FilePathRole).toString();
  70. QUrl url = QUrl::fromLocalFile(path);
  71. if (!model->isDir(index))
  72. return;
  73. OpenStrategy *strategy = OpenStrategy::defaultStrategy();
  74. if (!strategy)
  75. return;
  76. strategy->open(url);
  77. }
  78. void FileSystemToolWidget::onUrlChanged(const QUrl &url)
  79. {
  80. if (!url.isLocalFile())
  81. return;
  82. FileSystemToolModel *model = static_cast<FileSystemToolModel *>(this->model());
  83. FileSystemModel *fileSystemModel = model->fileSystemModel();
  84. QString path = url.toLocalFile();
  85. QModelIndex index = fileSystemModel->index(path);
  86. if (m_view->currentIndex() == index)
  87. return;
  88. m_view->collapseAll();
  89. m_view->setCurrentIndex(index);
  90. if (fileSystemModel->isDir(index))
  91. m_view->expand(index);
  92. }
  93. void FileSystemToolWidget::open()
  94. {
  95. QModelIndex index = m_view->currentIndex();
  96. const FileSystemModel *model = qobject_cast<const FileSystemModel *>(index.model());
  97. QString path = index.data(QFileSystemModel::FilePathRole).toString();
  98. QUrl url = QUrl::fromLocalFile(path);
  99. if (!model->isDir(index)) {
  100. QDesktopServices::openUrl(url);
  101. return;
  102. }
  103. OpenStrategy *strategy = OpenStrategy::defaultStrategy();
  104. if (!strategy)
  105. return;
  106. strategy->open(url);
  107. }
  108. void FileSystemToolWidget::openStrategy()
  109. {
  110. QAction *action = qobject_cast<QAction *>(sender());
  111. if (!action)
  112. return;
  113. QByteArray id = action->objectName().toUtf8();
  114. OpenStrategy *strategy = OpenStrategy::strategy(id);
  115. if (!strategy)
  116. return;
  117. QModelIndex index = m_view->currentIndex();
  118. if (!index.isValid())
  119. return;
  120. QString path = index.data(QFileSystemModel::FilePathRole).toString();
  121. QUrl url = QUrl::fromLocalFile(path);
  122. strategy->open(url);
  123. }
  124. void FileSystemToolWidget::contextMenuEvent(QContextMenuEvent *event)
  125. {
  126. QModelIndex index = m_view->currentIndex();
  127. if (!index.isValid())
  128. return;
  129. QString path = index.data(QFileSystemModel::FilePathRole).toString();
  130. QUrl url = QUrl::fromLocalFile(path);
  131. QMenu menu;
  132. foreach (OpenStrategy *strategy, OpenStrategy::strategies()) {
  133. if (!strategy->canOpen(QList<QUrl>() << url))
  134. continue;
  135. QAction *action = new QAction(&menu);
  136. action->setToolTip(strategy->toolTip());
  137. action->setText(strategy->text());
  138. action->setShortcut(strategy->keySequence());
  139. action->setObjectName(strategy->id());
  140. connect(action, SIGNAL(triggered()), SLOT(openStrategy()));
  141. menu.addAction(action);
  142. }
  143. menu.exec(event->globalPos());
  144. }