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