swift
viewdbobjects.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QAction>
7 #include <QHBoxLayout>
8 #include <QIntValidator>
9 #include <QLabel>
10 #include <QLineEdit>
11 #include <QWidgetAction>
12 
13 #include "gui/menus/menuaction.h"
24 #include "misc/countrylist.h"
25 #include "misc/icons.h"
29 
30 using namespace swift::misc;
31 using namespace swift::gui;
32 using namespace swift::gui::models;
33 using namespace swift::gui::menus;
34 
35 namespace swift::gui::views
36 {
37  template <class T>
39  {
40  // void
41  CViewBaseNonTemplate::m_enabledLoadIndicator = true; // indicator for DB views
42  }
43 
44  template <class T>
46  {
47  return this->container().latestObject();
48  }
49 
50  template <class T>
52  {
53  return this->container().oldestObject();
54  }
55 
56  template <class T>
58  {
59  const QSet<KeyType> set({ key });
60  return this->selectDbKeys(set) > 0;
61  }
62 
63  template <class T>
64  int CViewWithDbObjects<T>::selectDbKeys(const QSet<KeyType> &keys)
65  {
66  if (keys.isEmpty()) { return 0; }
67  this->clearSelection();
68  int r = -1;
69  QSet<int> rows;
71  {
72  r++;
73  if (!obj.hasValidDbKey()) { continue; }
74  if (keys.contains(obj.getDbKey())) { rows.insert(r); }
75  }
76  return this->selectRows(rows);
77  }
78 
79  template <class T>
80  QSet<typename CViewWithDbObjects<T>::KeyType> CViewWithDbObjects<T>::selectedDbKeys() const
81  {
82  if (!this->hasSelection()) { return QSet<KeyType>(); }
83  const ContainerType selected(this->selectedObjects());
84  return selected.toDbKeySet();
85  }
86 
87  template <class T>
88  int CViewWithDbObjects<T>::removeDbKeys(const QSet<KeyType> &keys)
89  {
90  if (keys.isEmpty()) { return 0; }
91  if (this->isEmpty()) { return 0; }
92 
93  ContainerType newObjects(this->container());
94  int delta = newObjects.removeObjectsWithKeys(keys);
95  if (delta > 0) { this->updateContainerMaybeAsync(newObjects); }
96  return delta;
97  }
98 
99  template <class T>
101  {
102  if (container.isEmpty()) { return 0; }
103  ContainerType copy(this->container());
104  int c = copy.replaceOrAddObjectsByKey(container);
105  if (c == 0) { return 0; }
106  this->updateContainerMaybeAsync(copy);
107  return c;
108  }
109 
110  template <class T>
112  {
113  if (selectedObjects.isEmpty()) { return; }
114  this->selectDbKeys(selectedObjects.toDbKeySet());
115  }
116 
117  template <class T>
119  {
120  // extensions would go here
122  }
123 
124  template <class T>
127  {
128  // void
129  }
130 
131  template <class T>
133  {
134  if (this->m_menus.testFlag(CViewBaseNonTemplate::MenuOrderable) && this->hasSelection())
135  {
136  const int maxOrder = this->rowCount() - 1;
137  CMenuAction menu = menuActions.addMenuViewOrder();
138  if (m_menuActions.isEmpty())
139  {
140  // predefine menus
141  m_menuActions = QList<QAction *>({ nullptr, nullptr, nullptr, nullptr });
142 
143  if (!m_menuActions[0])
144  {
145  m_frame = new QFrame(this);
146  QHBoxLayout *layout = new QHBoxLayout(m_frame);
147  layout->setContentsMargins(2, 2, 2, 2);
148  m_frame->setLayout(layout);
149  m_leOrder = new QLineEdit(m_frame);
150  QLabel *icon = new QLabel(m_frame);
151  icon->setPixmap(menu.getPixmap());
152  layout->addWidget(icon);
153  QLabel *label = new QLabel(m_frame);
154  label->setText("Order:");
155  layout->addWidget(label);
156  layout->addWidget(m_leOrder);
157  m_validator = new QIntValidator(0, maxOrder, this);
158  m_leOrder->setValidator(m_validator);
159  QWidgetAction *orderAction = new QWidgetAction(this);
160  orderAction->setDefaultWidget(m_frame);
161  QObject::connect(m_leOrder, &QLineEdit::returnPressed, this,
163  m_menuActions[0] = orderAction;
164  }
165  }
166 
167  m_validator->setRange(0, maxOrder);
168  m_leOrder->setPlaceholderText("New order 0-" + QString::number(maxOrder));
169 
170  menuActions.addAction(m_menuActions[0], CMenuAction::pathViewOrder());
171  m_menuActions[1] =
172  menuActions.addAction(CIcons::arrowMediumNorth16(), "To top", CMenuAction::pathViewOrder(),
174  m_menuActions[2] =
175  menuActions.addAction(CIcons::arrowMediumSouth16(), "To bottom", CMenuAction::pathViewOrder(),
177  m_menuActions[3] =
178  menuActions.addAction(CIcons::arrowMediumWest16(), "Freeze current order", CMenuAction::pathViewOrder(),
180  }
182  }
183 
184  template <class T>
186  {
187  if (selectedObjects.isEmpty()) { return; }
188  this->selectDbKeys(selectedObjects.toDbKeySet());
189  }
190 
191  template <class T>
193  {
194  if (this->isEmpty()) { return; }
195  const ContainerType objs(this->selectedObjects());
196  if (objs.isEmpty()) { return; }
197  this->m_model->moveItems(objs, order);
198  }
199 
200  template <class T>
202  {
203  this->moveSelectedItems(0);
204  }
205 
206  template <class T>
208  {
209  int c = this->model()->rowCount() - 1;
210  if (c >= 0) { this->moveSelectedItems(c); }
211  }
212 
213  template <class T>
215  {
216  if (this->isEmpty()) { return; }
217  QLineEdit *le = qobject_cast<QLineEdit *>(QObject::sender());
218  if (!le || le->text().isEmpty()) { return; }
219  const int order = le->text().toInt();
220  this->moveSelectedItems(order);
221  }
222 
223  template <class T>
225  {
226  ContainerType objects = this->container();
227  objects.freezeOrder();
228  this->updateContainerAsync(objects, false);
229  }
230 
231  // see here for the reason of thess forward instantiations
232  // https://isocpp.org/wiki/faq/templates#separate-template-fn-defn-from-decl
233 
243 
244 } // namespace swift::gui::views
Wraps a QAction with extra metadata to allow proper sorting for a QMenu.
Definition: menuaction.h:26
QPixmap getPixmap() const
Icon as pixmap.
Definition: menuaction.cpp:60
Bunch of CMenuAction objects.
Definition: menuaction.h:384
CMenuAction addAction(const CMenuAction &menuAction)
Add menu action.
Definition: menuaction.cpp:210
CMenuAction addMenuViewOrder()
View order menu.
Definition: menuaction.cpp:422
Base class for views with DB objects also orderable (based on swift::misc::IOrderableList )
Definition: viewdbobjects.h:79
virtual void customMenu(swift::gui::menus::CMenuActions &menuActions)
Method creating the menu.
virtual void selectObjects(const ContainerType &selectedObjects)
Select by DB keys.
COrderableViewWithDbObjects(QWidget *parent=nullptr)
Constructor.
void freezeCurrentOrder()
Current order set as order.
void moveSelectedItems(int order)
Move selected items.
Base class for views.
Definition: viewbase.h:648
typename T::ObjectType ObjectType
Model container element type.
Definition: viewbase.h:660
typename T::ContainerType ContainerType
Model container type.
Definition: viewbase.h:657
virtual void customMenu(menus::CMenuActions &menuActions)
Method creating the menu.
Definition: viewbase.cpp:680
bool m_enabledLoadIndicator
loading indicator enabled/disabled
Definition: viewbase.h:593
@ MenuOrderable
items can be ordered (if container is swift::misc::IOrderableList
Definition: viewbase.h:131
Base class for views with DB objects.
Definition: viewdbobjects.h:30
int removeDbKeys(const QSet< KeyType > &keys)
Remove keys.
typename T::ObjectType ObjectType
Model container element type.
Definition: viewdbobjects.h:39
typename T::KeyType KeyType
Model DB key type.
Definition: viewdbobjects.h:42
ObjectType oldestObject() const
Get oldets object.
int replaceOrAddObjectsByKey(const ContainerType &container)
Update or insert data (based on DB key)
virtual void selectObjects(const ContainerType &selectedObjects)
Select by DB keys.
bool selectDbKey(const KeyType &key)
Select given DB key.
int selectDbKeys(const QSet< KeyType > &keys)
Select given DB keys.
ObjectType latestObject() const
Get latest object.
QSet< KeyType > selectedDbKeys() const
Get selected DB keys.
virtual void customMenu(swift::gui::menus::CMenuActions &menuActions)
Method creating the menu.
Models to be used with views, mainly QTableView.
Views, mainly QTableView.
GUI related classes.
Free functions in swift::misc.