swift
Loading...
Searching...
No Matches
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
30using namespace swift::misc;
31using namespace swift::gui;
32using namespace swift::gui::models;
33using namespace swift::gui::menus;
34
35namespace swift::gui::views
36{
37 template <class T>
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>
65 {
66 if (keys.isEmpty())
67 {
68 return 0;
69 }
71 int r = -1;
72 QSet<int> rows;
74 {
75 r++;
76 if (!obj.hasValidDbKey())
77 {
78 continue;
79 }
80 if (keys.contains(obj.getDbKey()))
81 {
82 rows.insert(r);
83 }
84 }
85 return this->selectRows(rows);
86 }
87
88 template <class T>
90 {
91 if (!this->hasSelection())
92 {
93 return QSet<KeyType>();
94 }
95 const ContainerType selected(this->selectedObjects());
96 return selected.toDbKeySet();
97 }
99 template <class T>
102 if (keys.isEmpty())
103 {
104 return 0;
105 }
106 if (this->isEmpty())
108 return 0;
109 }
111 ContainerType newObjects(this->container());
112 int delta = newObjects.removeObjectsWithKeys(keys);
113 if (delta > 0)
114 {
115 this->updateContainerMaybeAsync(newObjects);
117 return delta;
118 }
119
120 template <class T>
122 {
123 if (container.isEmpty())
124 {
125 return 0;
126 }
127 ContainerType copy(this->container());
128 int c = copy.replaceOrAddObjectsByKey(container);
129 if (c == 0)
130 {
131 return 0;
132 }
134 return c;
135 }
136
137 template <class T>
139 {
140 if (selectedObjects.isEmpty())
141 {
142 return;
143 }
144 this->selectDbKeys(selectedObjects.toDbKeySet());
145 }
146
147 template <class T>
149 {
150 // extensions would go here
152 }
153
154 template <class T>
160
161 template <class T>
163 {
164 if (this->m_menus.testFlag(CViewBaseNonTemplate::MenuOrderable) && this->hasSelection())
165 {
166 const int maxOrder = this->rowCount() - 1;
167 CMenuAction menu = menuActions.addMenuViewOrder();
168 if (m_menuActions.isEmpty())
169 {
170 // predefine menus
171 m_menuActions = QList<QAction *>({ nullptr, nullptr, nullptr, nullptr });
172
173 if (!m_menuActions[0])
174 {
175 m_frame = new QFrame(this);
176 auto *layout = new QHBoxLayout(m_frame);
177 layout->setContentsMargins(2, 2, 2, 2);
178 m_frame->setLayout(layout);
179 m_leOrder = new QLineEdit(m_frame);
180 auto *icon = new QLabel(m_frame);
181 icon->setPixmap(menu.getPixmap());
182 layout->addWidget(icon);
183 auto *label = new QLabel(m_frame);
184 label->setText("Order:");
185 layout->addWidget(label);
186 layout->addWidget(m_leOrder);
187 m_validator = new QIntValidator(0, maxOrder, this);
188 m_leOrder->setValidator(m_validator);
189 auto *orderAction = new QWidgetAction(this);
190 orderAction->setDefaultWidget(m_frame);
193 m_menuActions[0] = orderAction;
194 }
195 }
196
197 m_validator->setRange(0, maxOrder);
198 m_leOrder->setPlaceholderText("New order 0-" + QString::number(maxOrder));
199
200 menuActions.addAction(m_menuActions[0], CMenuAction::pathViewOrder());
201 m_menuActions[1] =
204 m_menuActions[2] =
207 m_menuActions[3] =
208 menuActions.addAction(CIcons::arrowMediumWest16(), "Freeze current order", CMenuAction::pathViewOrder(),
210 }
212 }
213
214 template <class T>
216 {
217 if (selectedObjects.isEmpty())
218 {
219 return;
220 }
221 this->selectDbKeys(selectedObjects.toDbKeySet());
222 }
223
224 template <class T>
226 {
227 if (this->isEmpty())
228 {
229 return;
230 }
231 const ContainerType objs(this->selectedObjects());
232 if (objs.isEmpty())
233 {
234 return;
235 }
236 this->m_model->moveItems(objs, order);
237 }
238
239 template <class T>
244
245 template <class T>
247 {
248 int c = this->model()->rowCount() - 1;
249 if (c >= 0)
250 {
251 this->moveSelectedItems(c);
252 }
253 }
254
255 template <class T>
257 {
258 if (this->isEmpty())
259 {
260 return;
261 }
263 if (!le || le->text().isEmpty())
264 {
265 return;
266 }
267 const int order = le->text().toInt();
268 this->moveSelectedItems(order);
269 }
270
271 template <class T>
273 {
274 ContainerType objects = this->container();
275 objects.freezeOrder();
276 this->updateContainerAsync(objects, false);
277 }
278
279 // see here for the reason of thess forward instantiations
280 // https://isocpp.org/wiki/faq/templates#separate-template-fn-defn-from-decl
281
291
292} // namespace swift::gui::views
Wraps a QAction with extra metadata to allow proper sorting for a QMenu.
Definition menuaction.h:26
static const QString & pathViewOrder()
Order submenus.
Definition menuaction.h:295
QPixmap getPixmap() const
Icon as pixmap.
Bunch of CMenuAction objects.
Definition menuaction.h:384
CMenuAction addAction(const CMenuAction &menuAction)
Add menu action.
CMenuAction addMenuViewOrder()
View order menu.
Base class for views with DB objects also orderable (based on swift::misc::IOrderableList ).
typename models::CAircraftModelListModel::ContainerType ContainerType
void customMenu(swift::gui::menus::CMenuActions &menuActions)
Method creating the menu.
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.
bool isEmpty() const
Empty?
Definition viewbase.cpp:481
void updateContainerMaybeAsync(const ContainerType &container, bool sort=true, bool resize=true)
Definition viewbase.cpp:120
ContainerType selectedObjects() const
Selected objects.
Definition viewbase.cpp:267
ModelClass * m_model
corresponding model
Definition viewbase.h:886
int rowCount() const
Elements in container.
Definition viewbase.cpp:467
CViewBase(QWidget *parent, ModelClass *model=nullptr)
Constructor.
Definition viewbase.cpp:31
swift::misc::CWorker * updateContainerAsync(const ContainerType &container, bool sort=true, bool resize=true)
Update whole container in background.
Definition viewbase.cpp:95
const ContainerType & container() const
Access to container.
Definition viewbase.cpp:221
const ContainerType & containerOrFilteredContainer(bool *filtered=nullptr) const
Full container or cached filtered container as approproiate.
Definition viewbase.cpp:260
void copy()
Clipboard cut/copy/paste.
Definition viewbase.cpp:922
void customMenu(menus::CMenuActions &menuActions)
Method creating the menu.
Definition viewbase.cpp:785
Menu m_menus
Default menu settings.
Definition viewbase.h:603
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
bool hasSelection() const
Selection (selected rows).
Base class for views with DB objects.
int removeDbKeys(const QSet< KeyType > &keys)
Remove keys.
typename T::ObjectType ObjectType
Model container element type.
typename T::KeyType KeyType
Model DB key type.
ObjectType oldestObject() const
Get oldets object.
int replaceOrAddObjectsByKey(const ContainerType &container)
Update or insert data (based on DB key).
void selectObjects(const ContainerType &selectedObjects)
Select by DB keys.
typename T::ContainerType ContainerType
Model container type.
bool selectDbKey(const KeyType &key)
Select given DB key.
CViewWithDbObjects(QWidget *parent=nullptr)
Constructor.
ObjectType latestObject() const
Get latest object.
QSet< KeyType > selectedDbKeys() const
Get selected DB keys.
void customMenu(swift::gui::menus::CMenuActions &menuActions)
Method creating the menu.
static const QPixmap & arrowMediumSouth16()
Arrow.
static const QPixmap & arrowMediumWest16()
Arrow.
static const QPixmap & arrowMediumNorth16()
Arrow.
Models to be used with views, mainly QTableView.
Views, mainly QTableView.
GUI related classes.
Free functions in swift::misc.
QAbstractItemModel * model() const const
void returnPressed()
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
T qobject_cast(QObject *object)
QObject * sender() const const
bool contains(const QSet< T > &other) const const
QSet< T >::iterator insert(QSet< T >::const_iterator it, const T &value)
bool isEmpty() const const
QString number(double n, char format, int precision)
QLayout * layout() const const