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 <QComboBox>
25 #include <QInputDialog>
26 #include <QListView>
27 #include <QStandardItemModel>
28 #include <QTableWidget>
29 #include <QStringList>
30 
31 QString CommonGUI::askString(const QString& title, const QString& label,
32  bool* ok, const QString& defaultString)
33 {
34  return QInputDialog::getText(NULL, title,label, QLineEdit::Normal,
35  defaultString, ok);
36 }
37 
38 QList<bool> CommonGUI::listViewStandardItemsToBoolList(QListView* listview)
39 {
40  QList<bool> list;
41  QStandardItemModel* model = static_cast<QStandardItemModel*>(
42  listview->model());
43  for (int i = 0; i < model->rowCount(); ++i)
44  {
45  list << (model->item(i)->checkState() == Qt::Checked);
46  }
47 
48  return list;
49 }
50 
51 QStringList CommonGUI::listViewStandardItemsToStringList(QListView* listview)
52 {
53  QStringList list;
54  QStandardItemModel* model = static_cast<QStandardItemModel*>(
55  listview->model());
56  for (int i = 0; i < model->rowCount(); ++i)
57  {
58  list << model->item(i)->text();
59  }
60 
61  return list;
62 }
63 
64 void CommonGUI::removeSelectedRowsFromQTableWidget(QTableWidget* table)
65 {
66  // Rows can't be just deleted with items from selectedItems()
67  // because the program will crash. This solution is so stupid
68  // that there must be another one, but nobody knows...
69  QMap<int, QTableWidgetItem*> uniqueRowsItems;
70  foreach (QTableWidgetItem* item, table->selectedItems())
71  {
72  uniqueRowsItems.insert(item->row(), item);
73  }
74  foreach (QTableWidgetItem* item, uniqueRowsItems.values())
75  {
76  int row = table->row(item);
77  if (row >= 0)
78  {
79  table->removeRow(row);
80  }
81  }
82 }
83 
85  bool bSelectNextItem)
86 {
87  QItemSelectionModel* selModel = view->selectionModel();
88  QModelIndexList indexList = selModel->selectedIndexes();
89  selModel->clear();
90 
91  QStandardItemModel* model = static_cast<QStandardItemModel*>(view->model());
92  QList<QStandardItem*> itemList;
93  int lowestRemovedRow = 0;
94  for (int i = 0; i < indexList.count(); ++i)
95  {
96  const QModelIndex& index = indexList[i];
97  itemList << model->itemFromIndex(index);
98  if (index.row() > lowestRemovedRow)
99  {
100  lowestRemovedRow = index.row();
101  }
102  }
103 
104  for (int i = 0; i < itemList.count(); ++i)
105  {
106  QModelIndex index = model->indexFromItem(itemList[i]);
107  model->removeRow(index.row());
108  }
109 
110  if (bSelectNextItem && !indexList.isEmpty())
111  {
112  int selectRowIdx = lowestRemovedRow;
113  selectRowIdx -= indexList.size();
114 
115  if (selectRowIdx + 1 < model->rowCount())
116  {
117  ++selectRowIdx;
118  }
119 
120  if (selectRowIdx >= 0)
121  {
122  QModelIndex newIdx = model->index(selectRowIdx, 0);
123  selModel->select(newIdx, QItemSelectionModel::ClearAndSelect);
124  }
125  }
126 }
127 
128 void CommonGUI::setCurrentText(QComboBox *box, const QString &text)
129 {
130 #if QT_VERSION >= 0x050000
131  box->setCurrentText(text);
132 #else
133  if (box->isEditable())
134  {
135  box->setEditText(text);
136  }
137  else
138  {
139  for (int i = 0; i < box->count(); ++i)
140  {
141  if (box->itemText(i) == text)
142  {
143  box->setCurrentIndex(i);
144  break;
145  }
146  }
147  }
148 #endif
149 }
150 
151 void CommonGUI::stringListToStandardItemsListView(QListView* targetListview,
152  const QStringList& stringList)
153 {
154  QStandardItemModel* model = static_cast<QStandardItemModel*>(
155  targetListview->model());
156  model->removeRows(0, model->rowCount());
157 
158  foreach (const QString& str, stringList)
159  {
160  QStandardItem* pItem = new QStandardItem();
161  pItem->setText(str);
162  model->appendRow(pItem);
163  }
164 }
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:51
static void stringListToStandardItemsListView(QListView *targetListview, const QStringList &stringList)
Puts a list of strings into a QListView that uses QStandardItemModel.
Definition: commongui.cpp:151
static void removeSelectedRowsFromStandardItemView(QAbstractItemView *view, bool bSelectNextItem=false)
Removes all selected rows from a QAbstractItemView.
Definition: commongui.cpp:84
static QList< bool > listViewStandardItemsToBoolList(QListView *listview)
Definition: commongui.cpp:38
static void setCurrentText(QComboBox *box, const QString &text)
Qt4 compliant implementation of Qt5&#39;s QComboBox::setCurrentText().
Definition: commongui.cpp:128
static QString askString(const QString &title, const QString &label, bool *ok=NULL, const QString &defaultString="")
Calls QInputDialog::getText().
Definition: commongui.cpp:31