commongui.cpp
1 //------------------------------------------------------------------------------
2 // commongui.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (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
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "commongui.h"
24 #include <QInputDialog>
25 #include <QListView>
26 #include <QStandardItemModel>
27 #include <QTableWidget>
28 #include <QStringList>
29 
30 QString CommonGUI::askString(const QString& title, const QString& label,
31  bool* ok, const QString& defaultString)
32 {
33  return QInputDialog::getText(NULL, title,label, QLineEdit::Normal,
34  defaultString, ok);
35 }
36 
37 QList<bool> CommonGUI::listViewStandardItemsToBoolList(QListView* listview)
38 {
39  QList<bool> list;
40  QStandardItemModel* model = static_cast<QStandardItemModel*>(
41  listview->model());
42  for (int i = 0; i < model->rowCount(); ++i)
43  {
44  list << (model->item(i)->checkState() == Qt::Checked);
45  }
46 
47  return list;
48 }
49 
50 QStringList CommonGUI::listViewStandardItemsToStringList(QListView* listview)
51 {
52  QStringList list;
53  QStandardItemModel* model = static_cast<QStandardItemModel*>(
54  listview->model());
55  for (int i = 0; i < model->rowCount(); ++i)
56  {
57  list << model->item(i)->text();
58  }
59 
60  return list;
61 }
62 
63 void CommonGUI::removeSelectedRowsFromQTableWidget(QTableWidget* table)
64 {
65  // Rows can't be just deleted with items from selectedItems()
66  // because the program will crash. This solution is so stupid
67  // that there must be another one, but nobody knows...
68  QMap<int, QTableWidgetItem*> uniqueRowsItems;
69  foreach (QTableWidgetItem* item, table->selectedItems())
70  {
71  uniqueRowsItems.insert(item->row(), item);
72  }
73  foreach (QTableWidgetItem* item, uniqueRowsItems.values())
74  {
75  int row = table->row(item);
76  if (row >= 0)
77  {
78  table->removeRow(row);
79  }
80  }
81 }
82 
84  bool bSelectNextItem)
85 {
86  QItemSelectionModel* selModel = view->selectionModel();
87  QModelIndexList indexList = selModel->selectedIndexes();
88  selModel->clear();
89 
90  QStandardItemModel* model = static_cast<QStandardItemModel*>(view->model());
91  QList<QStandardItem*> itemList;
92  int lowestRemovedRow = 0;
93  for (int i = 0; i < indexList.count(); ++i)
94  {
95  const QModelIndex& index = indexList[i];
96  itemList << model->itemFromIndex(index);
97  if (index.row() > lowestRemovedRow)
98  {
99  lowestRemovedRow = index.row();
100  }
101  }
102 
103  for (int i = 0; i < itemList.count(); ++i)
104  {
105  QModelIndex index = model->indexFromItem(itemList[i]);
106  model->removeRow(index.row());
107  }
108 
109  if (bSelectNextItem && !indexList.isEmpty())
110  {
111  int selectRowIdx = lowestRemovedRow;
112  selectRowIdx -= indexList.size();
113 
114  if (selectRowIdx + 1 < model->rowCount())
115  {
116  ++selectRowIdx;
117  }
118 
119  if (selectRowIdx >= 0)
120  {
121  QModelIndex newIdx = model->index(selectRowIdx, 0);
122  selModel->select(newIdx, QItemSelectionModel::ClearAndSelect);
123  }
124  }
125 }
126 
127 void CommonGUI::stringListToStandardItemsListView(QListView* targetListview,
128  const QStringList& stringList)
129 {
130  QStandardItemModel* model = static_cast<QStandardItemModel*>(
131  targetListview->model());
132  model->removeRows(0, model->rowCount());
133 
134  foreach (const QString& str, stringList)
135  {
136  QStandardItem* pItem = new QStandardItem();
137  pItem->setText(str);
138  model->appendRow(pItem);
139  }
140 }
static QStringList listViewStandardItemsToStringList(QListView *listview)
Reads items from a QListView that uses QStandardItemModel and puts texts of these items into a list o...
Definition: commongui.cpp:50
static void stringListToStandardItemsListView(QListView *targetListview, const QStringList &stringList)
Puts a list of strings into a QListView that uses QStandardItemModel.
Definition: commongui.cpp:127
static void removeSelectedRowsFromStandardItemView(QAbstractItemView *view, bool bSelectNextItem=false)
Removes all selected rows from a QAbstractItemView.
Definition: commongui.cpp:83
static QList< bool > listViewStandardItemsToBoolList(QListView *listview)
Definition: commongui.cpp:37
static QString askString(const QString &title, const QString &label, bool *ok=NULL, const QString &defaultString="")
Calls QInputDialog::getText().
Definition: commongui.cpp:30