commongui.cpp
1 //------------------------------------------------------------------------------
2 // commongui.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library 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 GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; 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 <QStringList>
29 #include <QTableWidget>
30 
31 QString CommonGUI::askString(const QString &title, const QString &label,
32  bool *ok, const QString &defaultString)
33 {
34  return QInputDialog::getText(nullptr, title, label, QLineEdit::Normal,
35  defaultString, ok);
36 }
37 
38 QList<bool> CommonGUI::listViewStandardItemsToBoolList(QListView *listview)
39 {
40  QList<bool> list;
41  auto model = static_cast<QStandardItemModel *>(
42  listview->model());
43  for (int i = 0; i < model->rowCount(); ++i)
44  list << (model->item(i)->checkState() == Qt::Checked);
45 
46  return list;
47 }
48 
49 QStringList CommonGUI::listViewStandardItemsToStringList(QListView *listview)
50 {
51  QStringList list;
52  auto model = static_cast<QStandardItemModel *>(
53  listview->model());
54  for (int i = 0; i < model->rowCount(); ++i)
55  list << model->item(i)->text();
56 
57  return list;
58 }
59 
60 void CommonGUI::removeSelectedRowsFromQTableWidget(QTableWidget *table)
61 {
62  // Rows can't be just deleted with items from selectedItems()
63  // because the program will crash. This solution is so stupid
64  // that there must be another one, but nobody knows...
65  QMap<int, QTableWidgetItem *> uniqueRowsItems;
66  for (QTableWidgetItem *item : table->selectedItems())
67  {
68  uniqueRowsItems.insert(item->row(), item);
69  }
70  for (QTableWidgetItem *item : uniqueRowsItems.values())
71  {
72  int row = table->row(item);
73  if (row >= 0)
74  table->removeRow(row);
75  }
76 }
77 
79  bool bSelectNextItem)
80 {
81  QItemSelectionModel *selModel = view->selectionModel();
82  QModelIndexList indexList = selModel->selectedIndexes();
83  selModel->clear();
84 
85  auto model = static_cast<QStandardItemModel *>(view->model());
86  QList<QStandardItem *> itemList;
87  int lowestRemovedRow = 0;
88  for (int i = 0; i < indexList.count(); ++i)
89  {
90  const QModelIndex &index = indexList[i];
91  itemList << model->itemFromIndex(index);
92  if (index.row() > lowestRemovedRow)
93  lowestRemovedRow = index.row();
94  }
95 
96  for (int i = 0; i < itemList.count(); ++i)
97  {
98  QModelIndex index = model->indexFromItem(itemList[i]);
99  model->removeRow(index.row());
100  }
101 
102  if (bSelectNextItem && !indexList.isEmpty())
103  {
104  int selectRowIdx = lowestRemovedRow;
105  selectRowIdx -= indexList.size();
106 
107  if (selectRowIdx + 1 < model->rowCount())
108  ++selectRowIdx;
109 
110  if (selectRowIdx >= 0)
111  {
112  QModelIndex newIdx = model->index(selectRowIdx, 0);
113  selModel->select(newIdx, QItemSelectionModel::ClearAndSelect);
114  }
115  }
116 }
117 
118 void CommonGUI::stringListToStandardItemsListView(QListView *targetListview,
119  const QStringList &stringList)
120 {
121  auto model = static_cast<QStandardItemModel *>(
122  targetListview->model());
123  model->removeRows(0, model->rowCount());
124 
125  for (const QString &str : stringList)
126  {
127  auto pItem = new QStandardItem();
128  pItem->setText(str);
129  model->appendRow(pItem);
130  }
131 }