ctkActionsWidget.cpp 13 KB

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