ctkActionsWidget.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QAction>
  16. #include <QDebug>
  17. #include <QHeaderView>
  18. #include <QPainter>
  19. #include <QSortFilterProxyModel>
  20. #include <QStandardItem>
  21. #include <QStandardItemModel>
  22. #include <QTextDocument>
  23. #include <QTreeView>
  24. #include <QVBoxLayout>
  25. // CTK includes
  26. #include "ctkActionsWidget.h"
  27. //-----------------------------------------------------------------------------
  28. class ctkActionsWidgetPrivate
  29. {
  30. Q_DECLARE_PUBLIC(ctkActionsWidget);
  31. protected:
  32. ctkActionsWidget* const q_ptr;
  33. public:
  34. ctkActionsWidgetPrivate(ctkActionsWidget& object);
  35. void setupUI();
  36. void setupHeaders();
  37. void updateItems(QList<QStandardItem*>& items, QAction* action);
  38. QStandardItemModel* ActionsModel;
  39. ctkSortFilterActionsProxyModel* SortFilterActionsProxyModel;
  40. QTreeView* ActionsTreeView;
  41. };
  42. //-----------------------------------------------------------------------------
  43. ctkActionsWidgetPrivate::ctkActionsWidgetPrivate(ctkActionsWidget& object)
  44. :q_ptr(&object)
  45. {
  46. this->ActionsModel = 0;
  47. this->SortFilterActionsProxyModel = 0;
  48. this->ActionsTreeView = 0;
  49. }
  50. //-----------------------------------------------------------------------------
  51. void ctkActionsWidgetPrivate::setupUI()
  52. {
  53. Q_Q(ctkActionsWidget);
  54. this->ActionsModel = new QStandardItemModel(q);
  55. this->setupHeaders();
  56. this->SortFilterActionsProxyModel = new ctkSortFilterActionsProxyModel(q);
  57. this->SortFilterActionsProxyModel->setSourceModel(this->ActionsModel);
  58. this->ActionsTreeView = new QTreeView(q);
  59. QVBoxLayout* layout = new QVBoxLayout(q);
  60. layout->addWidget(this->ActionsTreeView);
  61. layout->setContentsMargins(0,0,0,0);
  62. q->setLayout(layout);
  63. this->ActionsTreeView->setModel(this->SortFilterActionsProxyModel);
  64. this->ActionsTreeView->setAlternatingRowColors(true);
  65. //this->ActionsTreeView->setItemDelegate(new ctkRichTextItemDelegate);
  66. }
  67. //-----------------------------------------------------------------------------
  68. void ctkActionsWidgetPrivate::setupHeaders()
  69. {
  70. this->ActionsModel->setColumnCount(4);
  71. QStringList headers;
  72. headers << "Action" << "Shortcut(s)" << "Context" << "Details";
  73. this->ActionsModel->setHorizontalHeaderLabels(headers);
  74. }
  75. //-----------------------------------------------------------------------------
  76. void ctkActionsWidgetPrivate
  77. ::updateItems(QList<QStandardItem*>& items, QAction* action)
  78. {
  79. Q_ASSERT(items.size() == 4);
  80. // Name
  81. QString actionText = action->text();
  82. if (actionText.indexOf('&') != -1)
  83. {
  84. actionText = actionText.remove(actionText.indexOf('&'),1); // remove mnemonic
  85. }
  86. items[ctkActionsWidget::NameColumn]->setText(actionText);
  87. items[ctkActionsWidget::NameColumn]->setIcon(action->icon());
  88. // Shortcut
  89. QStringList shortcuts;
  90. foreach(const QKeySequence& keySequence, action->shortcuts())
  91. {
  92. shortcuts << keySequence.toString(QKeySequence::NativeText);
  93. }
  94. items[ctkActionsWidget::ShortcutColumn]->setText(
  95. shortcuts.join("; "));
  96. // Context
  97. QString shortcutContext;
  98. switch (action->shortcutContext())
  99. {
  100. case Qt::WidgetShortcut:
  101. case Qt::WidgetWithChildrenShortcut:
  102. shortcutContext = "Widget";
  103. break;
  104. case Qt::WindowShortcut:
  105. case Qt::ApplicationShortcut:
  106. default:
  107. shortcutContext = "Application";
  108. }
  109. items[ctkActionsWidget::ContextColumn]->setText(shortcutContext);
  110. items[ctkActionsWidget::DetailsColumn]->setText(action->toolTip() != actionText
  111. ? action->toolTip() : QString(""));
  112. }
  113. //-----------------------------------------------------------------------------
  114. ctkActionsWidget::ctkActionsWidget(QWidget* parentWidget)
  115. :QWidget(parentWidget)
  116. , d_ptr(new ctkActionsWidgetPrivate(*this))
  117. {
  118. Q_D(ctkActionsWidget);
  119. d->setupUI();
  120. }
  121. //-----------------------------------------------------------------------------
  122. ctkActionsWidget::~ctkActionsWidget()
  123. {
  124. }
  125. //-----------------------------------------------------------------------------
  126. void ctkActionsWidget::addAction(QAction* action, const QString& group)
  127. {
  128. Q_D(ctkActionsWidget);
  129. QStandardItem* actionGroupItem = this->groupItem(group);
  130. Q_ASSERT(actionGroupItem);
  131. QList<QStandardItem*> actionItems;
  132. for (int i = 0; i < 4; ++i)
  133. {
  134. QStandardItem* item = new QStandardItem;
  135. item->setData(qVariantFromValue(qobject_cast<QObject*>(action)));
  136. item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  137. actionItems << item;
  138. }
  139. d->updateItems(actionItems, action);
  140. bool expandGroupItem = (actionGroupItem->rowCount() == 0);
  141. actionGroupItem->appendRow(actionItems);
  142. // if the group didn't exist yet or was empty, then open/expand it
  143. // automatcally to show its contents. If the group was not empty, then let
  144. // it as is (maybe the user closed/collapsed it for a good reason...
  145. if (expandGroupItem)
  146. {
  147. d->ActionsTreeView->expand(
  148. d->SortFilterActionsProxyModel->mapFromSource(d->ActionsModel->indexFromItem(actionGroupItem)));
  149. }
  150. d->ActionsTreeView->resizeColumnToContents(ctkActionsWidget::NameColumn);
  151. d->ActionsTreeView->resizeColumnToContents(ctkActionsWidget::DetailsColumn);
  152. connect(action, SIGNAL(changed()), this, SLOT(updateAction()));
  153. }
  154. //-----------------------------------------------------------------------------
  155. void ctkActionsWidget::addActions(QList<QAction*> actions, const QString& group)
  156. {
  157. Q_D(ctkActionsWidget);
  158. bool wasSortinEnabled = d->ActionsTreeView->isSortingEnabled();
  159. d->ActionsTreeView->setSortingEnabled(false);
  160. foreach(QAction* action, actions)
  161. {
  162. this->addAction(action, group);
  163. }
  164. d->ActionsTreeView->setSortingEnabled(wasSortinEnabled);
  165. }
  166. //-----------------------------------------------------------------------------
  167. void ctkActionsWidget::clear()
  168. {
  169. Q_D(ctkActionsWidget);
  170. d->ActionsModel->clear();
  171. d->setupHeaders();
  172. }
  173. //-----------------------------------------------------------------------------
  174. QStandardItem* ctkActionsWidget::groupItem(const QString& group)
  175. {
  176. Q_D(ctkActionsWidget);
  177. if (group.isEmpty())
  178. {
  179. return d->ActionsModel->invisibleRootItem();
  180. }
  181. // check if the group already exists
  182. QList<QStandardItem *> foundGroup =
  183. d->ActionsModel->findItems(group);
  184. if (foundGroup.size() > 0)
  185. {
  186. return foundGroup[0];
  187. }
  188. QStandardItem* groupItem = new QStandardItem(group);
  189. groupItem->setFlags(Qt::ItemIsEnabled);
  190. d->ActionsModel->appendRow(groupItem);
  191. return groupItem;
  192. }
  193. //-----------------------------------------------------------------------------
  194. QStandardItemModel* ctkActionsWidget::model()const
  195. {
  196. Q_D(const ctkActionsWidget);
  197. return d->ActionsModel;
  198. }
  199. //-----------------------------------------------------------------------------
  200. void ctkActionsWidget::setActionsWithNoShortcutVisible(bool show)
  201. {
  202. Q_D(ctkActionsWidget);
  203. d->SortFilterActionsProxyModel->setActionsWithNoShortcutVisible(show);
  204. }
  205. //-----------------------------------------------------------------------------
  206. bool ctkActionsWidget::areActionsWithNoShortcutVisible()const
  207. {
  208. Q_D(const ctkActionsWidget);
  209. return d->SortFilterActionsProxyModel->areActionsWithNoShortcutVisible();
  210. }
  211. //-----------------------------------------------------------------------------
  212. void ctkActionsWidget::setMenuActionsVisible(bool show)
  213. {
  214. Q_D(ctkActionsWidget);
  215. d->SortFilterActionsProxyModel->setMenuActionsVisible(show);
  216. }
  217. //-----------------------------------------------------------------------------
  218. bool ctkActionsWidget::areMenuActionsVisible()const
  219. {
  220. Q_D(const ctkActionsWidget);
  221. return d->SortFilterActionsProxyModel->areMenuActionsVisible();
  222. }
  223. //-----------------------------------------------------------------------------
  224. void ctkActionsWidget::setSortColumn(int column)
  225. {
  226. Q_D(ctkActionsWidget);
  227. d->ActionsTreeView->sortByColumn(column, Qt::AscendingOrder);
  228. d->ActionsTreeView->setSortingEnabled(column != -1);
  229. }
  230. //-----------------------------------------------------------------------------
  231. int ctkActionsWidget::sortColumn()const
  232. {
  233. Q_D(const ctkActionsWidget);
  234. return d->ActionsTreeView->isSortingEnabled() ?
  235. d->ActionsTreeView->header()->sortIndicatorSection() : -1;
  236. }
  237. //-----------------------------------------------------------------------------
  238. void ctkActionsWidget::updateAction()
  239. {
  240. Q_D(ctkActionsWidget);
  241. QAction* action = qobject_cast<QAction*>(this->sender());
  242. Q_ASSERT(action);
  243. QModelIndexList foundActions =
  244. d->ActionsModel->match(d->ActionsModel->index(0,0),
  245. Qt::UserRole + 1, qVariantFromValue(qobject_cast<QObject*>(action)),
  246. -1, Qt::MatchExactly | Qt::MatchRecursive);
  247. Q_ASSERT(foundActions.size());
  248. foreach (QModelIndex actionIndex, foundActions)
  249. {
  250. QModelIndex parentIndex = actionIndex.parent();
  251. QStandardItem* parent = parentIndex.isValid()
  252. ? d->ActionsModel->itemFromIndex(parentIndex)
  253. : d->ActionsModel->invisibleRootItem();
  254. int actionRow = actionIndex.row();
  255. Q_ASSERT(actionRow >= 0);
  256. QList<QStandardItem*> actionItems;
  257. for(int i = 0; i < 4; ++i)
  258. {
  259. actionItems << parent->child(actionRow, i);
  260. }
  261. d->updateItems(actionItems, action);
  262. }
  263. }
  264. //-----------------------------------------------------------------------------
  265. QTreeView* ctkActionsWidget::view()const
  266. {
  267. Q_D(const ctkActionsWidget);
  268. return d->ActionsTreeView;
  269. }
  270. //-----------------------------------------------------------------------------
  271. class ctkSortFilterActionsProxyModelPrivate
  272. {
  273. protected:
  274. ctkSortFilterActionsProxyModel* const q_ptr;
  275. public:
  276. ctkSortFilterActionsProxyModelPrivate(ctkSortFilterActionsProxyModel& object);
  277. bool ActionsWithNoShortcutVisible;
  278. bool MenuActionsVisible;
  279. };
  280. //-----------------------------------------------------------------------------
  281. ctkSortFilterActionsProxyModelPrivate::ctkSortFilterActionsProxyModelPrivate(ctkSortFilterActionsProxyModel& object)
  282. :q_ptr(&object)
  283. {
  284. this->ActionsWithNoShortcutVisible = true;
  285. this->MenuActionsVisible = true;
  286. }
  287. //-----------------------------------------------------------------------------
  288. ctkSortFilterActionsProxyModel::~ctkSortFilterActionsProxyModel()
  289. {
  290. }
  291. //-----------------------------------------------------------------------------
  292. ctkSortFilterActionsProxyModel::ctkSortFilterActionsProxyModel(QObject* parentObject)
  293. :QSortFilterProxyModel(parentObject)
  294. , d_ptr(new ctkSortFilterActionsProxyModelPrivate(*this))
  295. {
  296. }
  297. //-----------------------------------------------------------------------------
  298. void ctkSortFilterActionsProxyModel::setActionsWithNoShortcutVisible(bool visible)
  299. {
  300. Q_D(ctkSortFilterActionsProxyModel);
  301. d->ActionsWithNoShortcutVisible = visible;
  302. this->invalidateFilter();
  303. }
  304. //-----------------------------------------------------------------------------
  305. bool ctkSortFilterActionsProxyModel::areActionsWithNoShortcutVisible()const
  306. {
  307. Q_D(const ctkSortFilterActionsProxyModel);
  308. return d->ActionsWithNoShortcutVisible;
  309. }
  310. //-----------------------------------------------------------------------------
  311. void ctkSortFilterActionsProxyModel::setMenuActionsVisible(bool visible)
  312. {
  313. Q_D(ctkSortFilterActionsProxyModel);
  314. d->MenuActionsVisible = visible;
  315. this->invalidateFilter();
  316. }
  317. //-----------------------------------------------------------------------------
  318. bool ctkSortFilterActionsProxyModel::areMenuActionsVisible()const
  319. {
  320. Q_D(const ctkSortFilterActionsProxyModel);
  321. return d->MenuActionsVisible;
  322. }
  323. //-----------------------------------------------------------------------------
  324. bool ctkSortFilterActionsProxyModel::filterAcceptsRow(int source_row, const QModelIndex & source_parent) const
  325. {
  326. Q_D(const ctkSortFilterActionsProxyModel);
  327. QModelIndex shortcutIndex = this->sourceModel()->index(source_row, ctkActionsWidget::ShortcutColumn, source_parent);
  328. QStandardItem* shortcutItem = qobject_cast<QStandardItemModel*>(
  329. this->sourceModel())->itemFromIndex(shortcutIndex);
  330. QAction* action = shortcutItem ?
  331. qobject_cast<QAction*>(shortcutItem->data().value<QObject*>()) : 0;
  332. if (!action)
  333. {
  334. return true;
  335. }
  336. if (action->isSeparator())
  337. {
  338. return false;
  339. }
  340. if (action->text().isEmpty())
  341. {// not sure what the empty text actions are
  342. return false;
  343. }
  344. if (!d->ActionsWithNoShortcutVisible && shortcutItem->text().isEmpty())
  345. {
  346. return false;
  347. }
  348. if (!d->MenuActionsVisible && action->menu())
  349. {
  350. return false;
  351. }
  352. return true;
  353. }
  354. //---------------------------------------------------------------------------
  355. void ctkRichTextItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option,
  356. const QModelIndex &index) const
  357. {
  358. QStyleOptionViewItemV4 options = option;
  359. initStyleOption(&options, index);
  360. if (! Qt::mightBeRichText(options.text))
  361. {
  362. this->QStyledItemDelegate::paint(painter, option, index);
  363. return;
  364. }
  365. painter->save();
  366. QTextDocument doc;
  367. doc.setHtml(options.text);
  368. /* Call this to get the focus rect and selection background. */
  369. options.text = "";
  370. options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter, options.widget);
  371. /* Draw using our rich text document. */
  372. painter->translate(options.rect.left(), options.rect.top());
  373. QRect clip(0, 0, options.rect.width(), options.rect.height());
  374. doc.drawContents(painter, clip);
  375. painter->restore();
  376. }
  377. //---------------------------------------------------------------------------
  378. QSize ctkRichTextItemDelegate::sizeHint(const QStyleOptionViewItem & option,
  379. const QModelIndex & index)const
  380. {
  381. QStyleOptionViewItemV4 options = option;
  382. initStyleOption(&options, index);
  383. if (! Qt::mightBeRichText(options.text))
  384. {
  385. return this->QStyledItemDelegate::sizeHint(option, index);;
  386. }
  387. QTextDocument doc;
  388. doc.setHtml(options.text);
  389. doc.setTextWidth(options.rect.width());
  390. return QSize(doc.idealWidth(), doc.size().height());
  391. }