/src/rotatedheader.cpp

https://code.google.com/p/dwarftherapist/ · C++ · 217 lines · 150 code · 23 blank · 44 comment · 30 complexity · 4b5ade75c6d595db9607524236be0d59 MD5 · raw file

  1. /*
  2. Dwarf Therapist
  3. Copyright (c) 2009 Trey Stout (chmod)
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #include "rotatedheader.h"
  21. #include "dwarfmodel.h"
  22. #include "dwarfmodelproxy.h"
  23. #include "dwarftherapist.h"
  24. #include "defines.h"
  25. RotatedHeader::RotatedHeader(Qt::Orientation orientation, QWidget *parent)
  26. : QHeaderView(orientation, parent)
  27. , m_hovered_column(-1)
  28. {
  29. setClickable(true);
  30. setSortIndicatorShown(true);
  31. setMouseTracking(true);
  32. read_settings();
  33. connect(DT, SIGNAL(settings_changed()), this, SLOT(read_settings()));
  34. }
  35. void RotatedHeader::column_hover(int col) {
  36. updateSection(m_hovered_column);
  37. m_hovered_column = col;
  38. updateSection(col);
  39. }
  40. void RotatedHeader::read_settings() {
  41. QSettings *s = DT->user_settings();
  42. int cell_size = s->value("options/grid/cell_size", DEFAULT_CELL_SIZE).toInt();
  43. for(int i=1; i < count(); ++i) {
  44. if (!m_spacer_indexes.contains(i)) {
  45. resizeSection(i, cell_size);
  46. }
  47. }
  48. m_shade_column_headers = s->value("options/grid/shade_column_headers", true).toBool();
  49. }
  50. void RotatedHeader::paintSection(QPainter *p, const QRect &rect, int idx) const {
  51. if (!rect.isValid() || idx == 0)
  52. return QHeaderView::paintSection(p, rect, idx);
  53. QColor bg = model()->headerData(idx, Qt::Horizontal,
  54. Qt::BackgroundColorRole).value<QColor>();
  55. if (m_spacer_indexes.contains(idx)) {
  56. p->save();
  57. p->fillRect(rect, QBrush(bg));
  58. p->restore();
  59. return;
  60. }
  61. QStyleOptionHeader opt;
  62. opt.rect = rect;
  63. opt.orientation = Qt::Horizontal;
  64. opt.section = idx;
  65. opt.sortIndicator = QStyleOptionHeader::None;
  66. QStyle::State state = QStyle::State_None;
  67. if (isEnabled())
  68. state |= QStyle::State_Enabled;
  69. if (window()->isActiveWindow())
  70. state |= QStyle::State_Active;
  71. if (rect.contains(m_p))
  72. state |= QStyle::State_MouseOver;
  73. if (sortIndicatorSection() == idx) {
  74. //state |= QStyle::State_Sunken;
  75. if (sortIndicatorOrder() == Qt::AscendingOrder) {
  76. opt.sortIndicator = QStyleOptionHeader::SortDown;
  77. } else {
  78. opt.sortIndicator = QStyleOptionHeader::SortUp;
  79. }
  80. }
  81. if (m_hovered_column == idx) {
  82. state |= QStyle::State_MouseOver;
  83. }
  84. opt.state = state;
  85. style()->drawControl(QStyle::CE_HeaderSection, &opt, p);
  86. QBrush brush = QBrush(bg);
  87. if (m_shade_column_headers) {
  88. QLinearGradient g(rect.topLeft(), rect.bottomLeft());
  89. g.setColorAt(0.25, QColor(255, 255, 255, 10));
  90. g.setColorAt(1.0, bg);
  91. brush = QBrush(g);
  92. }
  93. if (idx > 0)
  94. p->fillRect(rect.adjusted(1,8,-1,-2), brush);
  95. if (sortIndicatorSection() == idx) {
  96. opt.rect = QRect(opt.rect.x() + opt.rect.width()/2 - 5, opt.rect.y(), 10, 8);
  97. style()->drawPrimitive(QStyle::PE_IndicatorHeaderArrow, &opt, p);
  98. }
  99. /* Draw a border around header if column has guides applied
  100. DwarfModelProxy *prox = static_cast<DwarfModelProxy*>(model());
  101. DwarfModel *dm = prox->get_dwarf_model();
  102. int col = dm->selected_col();
  103. if (dm->selected_col() == idx) {
  104. p->save();
  105. p->setPen(Qt::red);
  106. p->setBrush(Qt::NoBrush);
  107. p->drawRect(rect);
  108. p->restore();
  109. }
  110. */
  111. QString data = this->model()->headerData(idx, Qt::Horizontal).toString();
  112. p->save();
  113. p->setPen(Qt::black);
  114. p->setRenderHint(QPainter::Antialiasing);
  115. p->translate(rect.x(), rect.y());
  116. p->rotate(90);
  117. p->setFont(QFont("Verdana", 8));
  118. p->drawText(14, -4, data);
  119. p->restore();
  120. }
  121. void RotatedHeader::resizeSection(int logicalIndex, int size) {
  122. QHeaderView::resizeSection(logicalIndex, size);
  123. }
  124. void RotatedHeader::set_index_as_spacer(int idx) {
  125. m_spacer_indexes << idx;
  126. }
  127. void RotatedHeader::clear_spacers() {
  128. m_spacer_indexes.clear();
  129. }
  130. QSize RotatedHeader::sizeHint() const {
  131. return QSize(32, 150);
  132. }
  133. void RotatedHeader::mouseMoveEvent(QMouseEvent *e) {
  134. m_p = e->pos();
  135. QHeaderView::mouseMoveEvent(e);
  136. }
  137. void RotatedHeader::mousePressEvent(QMouseEvent *e) {
  138. m_p = e->pos();
  139. int idx = logicalIndexAt(e->pos());
  140. if (idx > 0 && idx < count() && e->button() == Qt::RightButton) {
  141. emit section_right_clicked(idx);
  142. }
  143. QHeaderView::mousePressEvent(e);
  144. }
  145. void RotatedHeader::leaveEvent(QEvent *e) {
  146. m_p = QPoint(-1, -1);
  147. QHeaderView::leaveEvent(e);
  148. }
  149. void RotatedHeader::contextMenuEvent(QContextMenuEvent *evt) {
  150. int idx = logicalIndexAt(evt->pos());
  151. if (idx == 0) { //name header
  152. QMenu *m = new QMenu(this);
  153. QAction *a = m->addAction(tr("Sort Alphabetically Ascending"), this, SLOT(sort_action()));
  154. a->setData(DwarfModelProxy::DSR_NAME_ASC);
  155. a = m->addAction(tr("Sort Alphabetically Descending"), this, SLOT(sort_action()));
  156. a->setData(DwarfModelProxy::DSR_NAME_DESC);
  157. a = m->addAction(tr("Sort by ID Ascending"), this, SLOT(sort_action()));
  158. a->setData(DwarfModelProxy::DSR_ID_ASC);
  159. a = m->addAction(tr("Sort by ID Descending"), this, SLOT(sort_action()));
  160. a->setData(DwarfModelProxy::DSR_ID_DESC);
  161. a = m->addAction(tr("Sort in Game Order"), this, SLOT(sort_action()));
  162. a->setData(DwarfModelProxy::DSR_GAME_ORDER);
  163. m->exec(viewport()->mapToGlobal(evt->pos()));
  164. } else {
  165. /* Don't do this yet
  166. // find out what set this is...
  167. QString set_name = model()->headerData(idx, Qt::Horizontal, Qt::UserRole).toString();
  168. QMenu *m = new QMenu(this);
  169. QAction *a = m->addAction(tr("Toggle Set %1").arg(set_name),
  170. this, SLOT(toggle_set_action()));
  171. a->setData(set_name);
  172. m->exec(viewport()->mapToGlobal(evt->pos()));
  173. */
  174. }
  175. }
  176. void RotatedHeader::sort_action() {
  177. QAction *sender = qobject_cast<QAction*>(QObject::sender());
  178. DwarfModelProxy::DWARF_SORT_ROLE role = static_cast<DwarfModelProxy::DWARF_SORT_ROLE>(sender->data().toInt());
  179. emit sort(0, role);
  180. }
  181. void RotatedHeader::toggle_set_action() {
  182. QAction *sender = qobject_cast<QAction*>(QObject::sender());
  183. QString set_name = sender->data().toString();
  184. for(int i = 1; i < count(); ++i) {
  185. QString col_set_name = model()->headerData(i, Qt::Horizontal,
  186. Qt::UserRole).toString();
  187. if (col_set_name == set_name) {
  188. hideSection(i);
  189. }
  190. }
  191. }