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 <QDialog>
26 #include <QInputDialog>
27 #include <QListView>
28 #include <QStandardItemModel>
29 #include <QStringList>
30 #include <QTableWidget>
31 
32 QString CommonGUI::askString(const QString &title, const QString &label,
33  bool *ok, const QString &defaultString)
34 {
35  return getText(nullptr, title, label, QLineEdit::Normal,
36  defaultString, ok);
37 }
38 
39 QString CommonGUI::getText(QWidget *parent, const QString &title, const QString &label,
40  QLineEdit::EchoMode mode, const QString &text, bool *ok)
41 {
42  bool _ok;
43  if (ok == nullptr)
44  ok = &_ok;
45  QInputDialog dialog(parent);
46  setupDialog(dialog);
47  dialog.setWindowTitle(title);
48  dialog.setLabelText(label);
49  dialog.setInputMode(QInputDialog::TextInput);
50  dialog.setTextValue(text);
51  dialog.setTextEchoMode(mode);
52  *ok = (dialog.exec() == QDialog::Accepted);
53  return (*ok) ? dialog.textValue() : QString();
54 }
55 
56 QList<bool> CommonGUI::listViewStandardItemsToBoolList(QListView *listview)
57 {
58  QList<bool> list;
59  auto model = static_cast<QStandardItemModel *>(
60  listview->model());
61  for (int i = 0; i < model->rowCount(); ++i)
62  list << (model->item(i)->checkState() == Qt::Checked);
63 
64  return list;
65 }
66 
67 QStringList CommonGUI::listViewStandardItemsToStringList(QListView *listview)
68 {
69  QStringList list;
70  auto model = static_cast<QStandardItemModel *>(
71  listview->model());
72  for (int i = 0; i < model->rowCount(); ++i)
73  list << model->item(i)->text();
74 
75  return list;
76 }
77 
78 void CommonGUI::removeSelectedRowsFromQTableWidget(QTableWidget *table)
79 {
80  // Rows can't be just deleted with items from selectedItems()
81  // because the program will crash. This solution is so stupid
82  // that there must be another one, but nobody knows...
83  QMap<int, QTableWidgetItem *> uniqueRowsItems;
84  for (QTableWidgetItem *item : table->selectedItems())
85  {
86  uniqueRowsItems.insert(item->row(), item);
87  }
88  for (QTableWidgetItem *item : uniqueRowsItems.values())
89  {
90  int row = table->row(item);
91  if (row >= 0)
92  table->removeRow(row);
93  }
94 }
95 
97  bool bSelectNextItem)
98 {
99  QItemSelectionModel *selModel = view->selectionModel();
100  QModelIndexList indexList = selModel->selectedIndexes();
101  selModel->clear();
102 
103  auto model = static_cast<QStandardItemModel *>(view->model());
104  QList<QStandardItem *> itemList;
105  int lowestRemovedRow = 0;
106  for (int i = 0; i < indexList.count(); ++i)
107  {
108  const QModelIndex &index = indexList[i];
109  itemList << model->itemFromIndex(index);
110  if (index.row() > lowestRemovedRow)
111  lowestRemovedRow = index.row();
112  }
113 
114  for (int i = 0; i < itemList.count(); ++i)
115  {
116  QModelIndex index = model->indexFromItem(itemList[i]);
117  model->removeRow(index.row());
118  }
119 
120  if (bSelectNextItem && !indexList.isEmpty())
121  {
122  int selectRowIdx = lowestRemovedRow;
123  selectRowIdx -= indexList.size();
124 
125  if (selectRowIdx + 1 < model->rowCount())
126  ++selectRowIdx;
127 
128  if (selectRowIdx >= 0)
129  {
130  QModelIndex newIdx = model->index(selectRowIdx, 0);
131  selModel->select(newIdx, QItemSelectionModel::ClearAndSelect);
132  }
133  }
134 }
135 
136 void CommonGUI::setupDialog(QDialog &dialog)
137 {
138  dialog.setWindowFlags(dialog.windowFlags() & (~Qt::WindowContextHelpButtonHint));
139 }
140 
141 void CommonGUI::stringListToStandardItemsListView(QListView *targetListview,
142  const QStringList &stringList)
143 {
144  auto model = static_cast<QStandardItemModel *>(
145  targetListview->model());
146  model->removeRows(0, model->rowCount());
147 
148  for (const QString &str : stringList)
149  {
150  auto pItem = new QStandardItem();
151  pItem->setText(str);
152  model->appendRow(pItem);
153  }
154 }