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