FreeDebks  1.0.3
 All Classes Files Functions Variables Friends Pages
FdViewResults.cpp
Go to the documentation of this file.
1 // --------------------------------------------------------------------
2 // Copyright © 2011-2013 Mathieu Schopfer
3 //
4 // This file is part of FreeDebks.
5 //
6 // FreeDebks is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // FreeDebks is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with FreeDebks. If not, see <http://www.gnu.org/licenses/>.
18 // --------------------------------------------------------------------
19 
20 #include "FdViewResults.hpp"
21 #include "../../data/FdModelResults.hpp"
22 #include "../../data/FdItemResults.hpp"
23 #include "../FdSubWindow.hpp"
24 #include "../FdMainWindow.hpp"
25 
27  QTableView(0), mSubwindow(parent)
28 {
29  mModel = mSubwindow->results();
30  setModel(mModel);
31 
32  setSelectionBehavior(QAbstractItemView::SelectRows);
33  setSelectionMode(QAbstractItemView::ExtendedSelection);
34  setDragDropMode(QAbstractItemView::InternalMove);
35  setDragEnabled(true);
36  setAcceptDrops(true);
37  setDropIndicatorShown(true);
38  setEditTriggers(DoubleClicked | EditKeyPressed);
39  QHeaderView* header = verticalHeader();
40  header->setDefaultSectionSize(16);
41  setVerticalHeader(header);
42 
43  setAlternatingRowColors(true);
44 
45  QSettings settings;
46  settings.beginGroup("ViewResults");
47  setColumnWidth(ResultsId, settings.value("id", 50).toInt());
48  setColumnWidth(ResultsLabel, settings.value("label", 300).toInt());
49  setColumnWidth(ResultsCalculation, settings.value("calculation", 500).toInt());
50  settings.endGroup();
51  horizontalHeader()->setResizeMode(ResultsResult, QHeaderView::Stretch);
52 
53  mDelegate = new FdDelegateResults(mModel);
54  setItemDelegate(mDelegate);
55 
56  setContextMenuPolicy(Qt::CustomContextMenu);
57  connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(contextMenu(const QPoint&)));
58 
59  mSearchTimer = new QTimer();
60  mSearchTimer->setInterval(qApp->keyboardInputInterval());
61  mSearchTimer->setSingleShot(true);
62  connect(mSearchTimer, SIGNAL(timeout()), this, SLOT(searchTimeout()));
63 }
64 
68 void FdViewResults::mouseReleaseEvent(QMouseEvent* event)
69 {
70  if(event->button() == Qt::LeftButton && !(indexAt(event->pos()).isValid()))
71  setCurrentIndex(QModelIndex());
72 
73  QList<int> rows = selectedRows();
74  if(rows.size() == 0)
75  {
76 
77  }
78  else if(rows.size() == 1)
79  {
80 
81  }
82  else
83  {
84 
85  }
86 
88 }
89 
93 QModelIndex FdViewResults::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
94 {
95  if(cursorAction == QAbstractItemView::MoveNext)
96  {
97  QModelIndex index = currentIndex();
98  if(index.column() == ResultsCalculation)
99  {
100  if(index.row() == model()->rowCount()-1)
101  return mModel->addCalculation(QModelIndex());
102  else
103  return mModel->index(index.row()+1, ResultsId);
104  }
105  }
106  else if(cursorAction == QAbstractItemView::MovePrevious)
107  {
108  QModelIndex index = currentIndex();
109  if(index.column() == ResultsId)
110  {
111  if(index.row())
112  return mModel->index(index.row()-1, ResultsCalculation);
113  else
114  return index;
115  }
116 
117  }
118  else if(cursorAction == QAbstractItemView::MoveHome && modifiers == Qt::ControlModifier)
119  {
120  clearSelection();
121  setCurrentIndex(mModel->index(0, 0));
122  }
123  else if(cursorAction == QAbstractItemView::MoveEnd && modifiers == Qt::ControlModifier)
124  {
125  clearSelection();
126  setCurrentIndex(mModel->index(mModel->rowCount()-1, ResultsColumnCount-1));
127  }
128 
129 
130  return QTableView::moveCursor(cursorAction, modifiers);
131 }
132 
138 void FdViewResults::keyboardSearch(const QString &search)
139 {
140  mSearchTimer->start();
141  mSearchText.append(search);
142  mainWindow->setDockText(tr("Searched string : %1", "Text displayed in the dock widget while keyboardsearching in views. The argument is the user input string to search.").arg(mSearchText));
143 }
144 
145 void FdViewResults::print(QPrinter &printer, QPainter &painter, const QRect &area, QRect& remainingArea, FdSubWindow *subwindow, int startPage)
146 {
147  const int rows = model()->rowCount();
148 
149  int pageNumber = 0;
150  bool titlePrinted = false;
151  bool headerPrinted = false;
152 
153  struct tableElement labelHeader;
154  labelHeader.width = 0.6*area.width();
155  labelHeader.text = tr("Label", "Results printed pages column header.");
156  labelHeader.alignement = Qt::AlignLeft;
157  labelHeader.color = Qt::black;
158 
159  struct tableElement previousHeader;
160  previousHeader.width = 0.2*area.width();
161  previousHeader.text = tr("Previous year", "Results printed pages column header.");
162  previousHeader.alignement = Qt::AlignRight;
163  previousHeader.color = Qt::black;
164 
165  struct tableElement resultHeader;
166  resultHeader.width = 0.2*area.width();
167  resultHeader.text = tr("Result", "Results printed pages column header.");
168  resultHeader.alignement = Qt::AlignRight;
169  resultHeader.color = Qt::black;
170 
171  QList<tableElement*> header;
172  header << &labelHeader << &previousHeader << &resultHeader;
173 
174  struct tableElement label = labelHeader;
175  struct tableElement previous = previousHeader;
176  struct tableElement result = resultHeader;
177 
178  QList<tableElement*> row;
179  row << &label << &previous << &result;
180 
181  FdItemResults_p item;
182 
183  int neededHeight = 1200 + rows*185;
184 
185  if(neededHeight > remainingArea.height() && neededHeight < 0.75*area.height())
186  remainingArea.setHeight(0);
187 
188  for(int i = 0; i < rows; ++i)
189  {
190  item = mModel->item(i);
191 
192  if(remainingArea.height() == 0)
193  {
194  printer.newPage();
195  ++pageNumber;
196  remainingArea = area;
197  subwindow->printHeaderFooter(printer, painter, tr("Results"), pageNumber+startPage);
198  headerPrinted = false;
199  }
200 
201  if(!titlePrinted)
202  {
203  FdSubWindow::printTitle(painter, remainingArea, tr("Results"));
204  titlePrinted = true;
205  }
206  if(!headerPrinted)
207  {
208  FdSubWindow::printColumnsHeader(painter, remainingArea, header);
209  headerPrinted = true;
210  }
211 
212 // painter.drawRect(area);
213 
214  label.text = item->id()+" "+item->label();
215  previous.text = mDelegate->displayText(item->previousResult());
216  result.text = mDelegate->displayText(item->result());
217  if(result.text.toDouble() < 0)
218  result.color = Qt::red;
219  else
220  result.color = Qt::black;
221 
222  FdSubWindow::printTableRow(painter, remainingArea, row);
223  }
224 }
225 
226 QList<int> FdViewResults::selectedRows() const
227 {
228  QModelIndexList indexes = selectedIndexes();
229  QList<int> rows;
230  for(int i = 0; i < indexes.size(); ++i)
231  {
232  if(indexes[i].column() == 0)
233  rows << indexes[i].row();
234  }
235  return rows;
236 }
237 
239 {
240  QSettings settings;
241  settings.beginGroup("ViewResults");
242  settings.setValue("id", columnWidth(ResultsId));
243  settings.setValue("label", columnWidth(ResultsLabel));
244  settings.setValue("calculation", columnWidth(ResultsCalculation));
245  settings.endGroup();
246 }
247 
248 void FdViewResults::edit(const QModelIndex &index)
249 {
250  setCurrentIndex(index);
251  QTableView::edit(index);
252 }
253 
257 void FdViewResults::setCurrentIndex(const QModelIndex &index)
258 {
259  clearSelection();
261 }
262 
263 void FdViewResults::currentChanged(const QModelIndex &current, const QModelIndex &previous)
264 {
265  mainWindow->enableRemoveAction(current.isValid());
266 
267  QTableView::currentChanged(current, previous);
268 }
269 
270 void FdViewResults::contextMenu(const QPoint &position)
271 {
272  QModelIndex index = currentIndex();
273  QMenu* menu = new QMenu();
274 
275  if(index.isValid())
276  {
277  menu->addActions(mainWindow->getContextMenuActionsEdit());
278  menu->addSeparator();
279  }
280  menu->addActions(mainWindow->getContextMenuActionsResults());
281  menu->exec(mapToGlobal(position));
282 }
283 
289 {
291  if(mSearchResults.size())
292  {
293  mCurrentResult = -1;
295  }
296  else
297  mainWindow->setDockText(tr("Sorry, no matches found.", "No matches found when searching item by text."));
298 
299  mSearchText = QString();
300 }
301 
303 {
304  if(mSearchResults.size())
305  {
306  if(mCurrentResult < mSearchResults.size() - 1)
307  {
308  ++ mCurrentResult;
310  }
311  else
312  {
313  mCurrentResult = -1;
315  }
316  }
317  else
318  mainWindow->setDockText(tr("Sorry, no matches found.", "No matches found when searching item by text."));
319 }
320 
322  QStyledItemDelegate(0), mModel(model)
323 {
324 }
325 
326 QString FdDelegateResults::displayText(const QVariant &value) const
327 {
328  return displayText(value, QLocale::system());
329 }
330 
334 QString FdDelegateResults::displayText(const QVariant &value, const QLocale &locale) const
335 {
336  if(value.type() == QVariant::Double)
337  return QLocale::system().toString(value.toDouble(), 'f', 2);
338 
339  return QStyledItemDelegate::displayText(value, locale);
340 }
341 
342 void FdDelegateResults::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
343 {
344  FdItemResults_p item = mModel->item(index.row());
345  QStyleOptionViewItem viewOption(option);
346 
347  if(!mModel->data(index, ValidityRole).toBool())
348  viewOption.palette.setColor(QPalette::Highlight, Qt::red);
349 
350  if(index.column() == ResultsResult && item->result() < 0)
351  viewOption.palette.setColor(QPalette::Text, Qt::red);
352 
353  QStyledItemDelegate::paint(painter, viewOption, index);
354 }