pluginsview.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*****************************************************************************
  2. * pluginsview.cpp - Code adapted from QtNote project *
  3. * Copyright (C) 2016 Sergey Il'inykh *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License along *
  16. * with this program; if not, write to the Free Software Foundation, Inc., *
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  18. *****************************************************************************/
  19. #include <QHeaderView>
  20. #include <QStyledItemDelegate>
  21. #include <QPainter>
  22. #include "application.h"
  23. #include "pluginsview.h"
  24. namespace QStarDict {
  25. class ButtonDelegate : public QStyledItemDelegate
  26. {
  27. Q_OBJECT
  28. enum ButtonRoles {
  29. ButtonRole = Qt::UserRole + 1
  30. };
  31. QModelIndex sunken;
  32. public:
  33. explicit ButtonDelegate(QObject *parent = 0) :
  34. QStyledItemDelegate(parent)
  35. {
  36. }
  37. // painting
  38. void paint(QPainter *painter,
  39. const QStyleOptionViewItem &option, const QModelIndex &index) const
  40. {
  41. QStyleOptionViewItem opt = option;
  42. initStyleOption(&opt, index);
  43. if (opt.icon.isNull()) {
  44. return;
  45. }
  46. painter->save();
  47. if (opt.state & QStyle::State_Selected) {
  48. painter->setPen(QPen(Qt::NoPen));
  49. if (opt.state & QStyle::State_Active) {
  50. painter->setBrush(QBrush(QPalette().highlight()));
  51. }
  52. else {
  53. painter->setBrush(QBrush(QPalette().color(QPalette::Inactive,
  54. QPalette::Highlight)));
  55. }
  56. painter->drawRect(opt.rect);
  57. }
  58. QStyleOptionButton buttonOption;
  59. buttonOption.icon = opt.icon;
  60. buttonOption.iconSize = option.decorationSize;
  61. buttonOption.text = opt.text;
  62. buttonOption.features = QStyleOptionButton::Flat;
  63. buttonOption.rect = opt.rect;
  64. buttonOption.state = QStyle::State_Enabled;
  65. if (index == sunken) {
  66. buttonOption.state |= QStyle::State_Sunken;
  67. }
  68. if (option.state & QStyle::State_MouseOver) {
  69. buttonOption.state |= (QStyle::State_Active | QStyle::State_MouseOver);
  70. }
  71. QApplication::style()->drawControl(QStyle::CE_PushButton,
  72. &buttonOption,
  73. painter);
  74. painter->restore();
  75. }
  76. bool editorEvent(QEvent *event,
  77. QAbstractItemModel *model,
  78. const QStyleOptionViewItem &option,
  79. const QModelIndex &index)
  80. {
  81. Q_UNUSED(model);
  82. Q_UNUSED(option);
  83. if(!(event->type() == QEvent::MouseButtonPress ||
  84. event->type() == QEvent::MouseButtonRelease)) {
  85. return true;
  86. }
  87. sunken = QModelIndex();
  88. if( event->type() == QEvent::MouseButtonPress) {
  89. sunken = index;
  90. }
  91. return true;
  92. }
  93. };
  94. PluginsView::PluginsView(QWidget *parent) :
  95. QTableView(parent)
  96. {
  97. verticalHeader()->hide();
  98. horizontalHeader()->hide();
  99. //setSelectionBehavior(QAbstractItemView::SelectRows);
  100. setSelectionMode(QAbstractItemView::NoSelection);
  101. setEditTriggers(NoEditTriggers);
  102. setShowGrid(false);
  103. }
  104. void PluginsView::configureColumns()
  105. {
  106. ButtonDelegate *btnsDelegate = new ButtonDelegate();
  107. setItemDelegateForColumn(2, btnsDelegate);
  108. setItemDelegateForColumn(3, btnsDelegate);
  109. horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
  110. horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
  111. horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
  112. horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
  113. }
  114. } // namespace QStarDict
  115. #include "pluginsview.moc"