Isis 3 Programmer Reference
TreeViewHeader.cpp
1 
7 /* SPDX-License-Identifier: CC0-1.0 */
8 
9 #include "IsisDebug.h"
10 
11 #include "TreeViewHeader.h"
12 
13 #include <iostream>
14 
15 #include <QAbstractItemModel>
16 #include <QFontMetrics>
17 #include <QLabel>
18 #include <QLinearGradient>
19 #include <QLocale>
20 #include <QPainter>
21 #include <QPen>
22 #include <QRect>
23 #include <QString>
24 #include <QVBoxLayout>
25 
26 #include "TreeView.h"
27 #include "TreeViewContent.h"
28 #include "AbstractTreeModel.h"
29 
30 
31 namespace Isis {
32  TreeViewHeader::TreeViewHeader(TreeViewContent *someContent,
33  QWidget *parent) : QWidget(parent), m_content(someContent) {
34  nullify();
35 
36  ASSERT(m_content);
37 
38  m_headerText = new QString("Header text");
39  m_filterProgress = 0;
40  m_filterProgressMin = 0;
41  m_filterProgressMax = 0;
42  m_rebuildProgress = 0;
43  m_rebuildProgressMin = 0;
44  m_rebuildProgressMax = 0;
45  m_active = false;
46  m_visibleCount = -1;
47  m_totalCount = -1;
48  }
49 
50 
51  TreeViewHeader::TreeViewHeader(const TreeViewHeader &other) {
52  nullify();
53 
54  m_headerText = new QString(*other.m_headerText);
55  }
56 
57 
58  TreeViewHeader::~TreeViewHeader() {
59  if (m_headerText) {
60  delete m_headerText;
61  m_headerText = NULL;
62  }
63  }
64 
65 
66  QSize TreeViewHeader::minimumSizeHint() const {
67  return QSize(QFontMetrics(font()).width(*m_headerText) + 15,
68  QFontMetrics(font()).height() + 6);
69  }
70 
71  QString TreeViewHeader::getText() {
72  ASSERT(m_headerText);
73  return *m_headerText;
74  }
75 
76 
77  void TreeViewHeader::setText(QString text) {
78  ASSERT(m_headerText);
79  *m_headerText = text;
80  updateGeometry();
81  update();
82  }
83 
84 
85  TreeViewHeader &TreeViewHeader::operator=(
86  const TreeViewHeader &other) {
87  if (this != &other) {
88  if (m_headerText) {
89  delete m_headerText;
90  m_headerText = NULL;
91  }
92  m_headerText = new QString;
93  *m_headerText = *other.m_headerText;
94  }
95 
96  return *this;
97  }
98 
99 
100  void TreeViewHeader::setActive(bool newActiveState) {
101  m_active = newActiveState;
102  }
103 
104 
105  void TreeViewHeader::handleFilterCountsChanged(
106  int visibleTopLevelItemCount, int topLevelItemCount) {
107  m_visibleCount = visibleTopLevelItemCount;
108  m_totalCount = topLevelItemCount;
109  updateGeometry();
110  update();
111  }
112 
113 
114  void TreeViewHeader::mouseReleaseEvent(QMouseEvent *event) {
115  setActive(true);
116  emit(activated());
117  update();
118  }
119 
120 
121  void TreeViewHeader::paintEvent(QPaintEvent *event) {
122  QPainter painter(this);
123  painter.setRenderHints(QPainter::Antialiasing |
124  QPainter::TextAntialiasing);
125  paintHeader(&painter, height());
126  painter.drawRect(0, 0, width(), height());
127  painter.end();
128  }
129 
130 
131  void TreeViewHeader::nullify() {
132  m_headerText = NULL;
133  }
134 
135 
136  void TreeViewHeader::paintHeader(QPainter *painter, int rowHeight) {
137  QRect rect(0, 0, width(), rowHeight);
138 
139  int x = rect.center().x();
140  QLinearGradient gradient(x, rect.top(), x, rect.bottom());
141 
142  //FIXME: selected needs to be member variable
143  bool selected = false;
144  QColor color = selected ? palette().highlight().color() :
145  palette().button().color();
146 
147  // create gradient and fill header area with it
148  int adjustment = 100;
149  m_active ? adjustment += 7 : adjustment -= 3;
150  gradient.setColorAt(0, color.darker(adjustment));
151  gradient.setColorAt(0.5, color.lighter(adjustment));
152  gradient.setColorAt(1, color.darker(adjustment));
153  painter->fillRect(rect, gradient);
154 
155  // Save off composition mode and brush, which will need to be restored
156  // after the progress is painted.
157  QBrush brush = painter->brush();
158  QPainter::CompositionMode compMode = painter->compositionMode();
159  painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
160 
161  // draw filter progress (if not at 100%)
162  painter->setBrush(QBrush(QColor(0, 70, 100, 30)));
163  paintProgress(painter, rect, m_filterProgressMin, m_filterProgressMax,
164  m_filterProgress);
165 
166  // draw rebuild progress (if not at 100%)
167  painter->setBrush(QBrush(QColor(100, 70, 0, 30)));
168  paintProgress(painter, rect, m_rebuildProgressMin, m_rebuildProgressMax,
169  m_rebuildProgress);
170 
171  // draw the header's text
172  painter->setBrush(brush);
173  painter->setCompositionMode(compMode);
174  painter->setPen(selected ? palette().highlightedText().color() :
175  palette().buttonText().color());
176 
177  QString title(*m_headerText);
178 
179  if (m_visibleCount >= 0) {
180  title += " (";
181 
182  title += QLocale().toString(m_visibleCount);
183 
184  title += " / ";
185  title += QLocale().toString(m_totalCount);
186  title += ")";
187  }
188 
189  painter->drawText(rect, title, QTextOption(Qt::AlignCenter));
190  }
191 
192 
193  void TreeViewHeader::paintProgress(QPainter *painter, const QRect &rect,
194  int min, int max, int value) {
195  // draw filter progress if not at 100%
196  double progressPercent = 1.0;
197  int progressRange = max - min;
198  if (progressRange > 0)
199  progressPercent = ((double)(value - min)) / progressRange;
200 
201  if (progressPercent < 1.0) {
202  QRect progressRect(rect);
203  progressRect.setWidth((int)(progressRect.width() * progressPercent));
204  painter->fillRect(progressRect, painter->brush());
205  }
206  }
207 
208 
209  void TreeViewHeader::updateFilterProgress(int newProgress) {
210  m_filterProgress = newProgress;
211  update();
212  }
213 
214 
215  void TreeViewHeader::updateFilterProgressRange(int min, int max) {
216  m_filterProgressMin = min;
217  m_filterProgressMax = max;
218  update();
219  }
220 
221 
222  void TreeViewHeader::updateRebuildProgress(int newProgress) {
223  m_rebuildProgress = newProgress;
224  update();
225  }
226 
227 
228  void TreeViewHeader::updateRebuildProgressRange(int min, int max) {
229  m_rebuildProgressMin = min;
230  m_rebuildProgressMax = max;
231  update();
232  }
233 }
QWidget
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16