/* Copyright (C) 2015 * swift project Community / Contributors * * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, * including this file, may be copied, modified, propagated, or distributed except according to the terms * contained in the LICENSE file. */ #include "viewdbobjects.h" #include "blackgui/models/allmodels.h" #include #include #include #include using namespace BlackMisc; using namespace BlackGui; using namespace BlackGui::Models; namespace BlackGui { namespace Views { template CViewWithDbObjects::CViewWithDbObjects(QWidget *parent) : CViewBase(parent) { // void } template ObjectType CViewWithDbObjects::latestObject() const { return this->container().latestObject(); } template ObjectType CViewWithDbObjects::oldestObject() const { return this->container().oldestObject(); } template void CViewWithDbObjects::selectDbKeys(const QList &keys) { if (keys.isEmpty()) { return; } this->clearSelection(); int r = -1; for (const ObjectType &obj : CViewBase::containerOrFilteredContainer()) { r++; if (!obj.hasValidDbKey()) { continue; } if (keys.contains(obj.getDbKey())) { this->selectRow(r); } } } template int CViewWithDbObjects::removeDbKeys(const QList &keys) { if (keys.isEmpty()) { return 0; } if (this->isEmpty()) { return 0; } ContainerType newObjects(this->container()); int delta = newObjects.removeObjectsWithKeys(keys); if (delta > 0) { this->updateContainerMaybeAsync(newObjects); } return delta; } template int CViewWithDbObjects::replaceOrAddObjectsByKey(const ContainerType &container) { if (container.isEmpty()) { return 0; } ContainerType copy(this->container()); int c = copy.replaceOrAddObjectsByKey(container); if (c == 0) { return 0; } this->updateContainerMaybeAsync(copy); return c; } template void CViewWithDbObjects::customMenu(QMenu &menu) const { if (this->m_menus.testFlag(CViewBaseNonTemplate::MenuHighlightDbData)) { QAction *a = menu.addAction(CIcons::database16(), "Highlight DB data", this, &CViewWithDbObjects::ps_toggleHighlightDbData); a->setCheckable(true); a->setChecked(this->derivedModel()->highlightDbData()); } CViewBase::customMenu(menu); } template void CViewWithDbObjects::ps_toggleHighlightDbData() { bool h = this->derivedModel()->highlightDbData(); this->derivedModel()->setHighlightDbData(!h); } template COrderableViewWithDbObjects::COrderableViewWithDbObjects(QWidget *parent) : CViewWithDbObjects::CViewWithDbObjects(parent) { // void } template void COrderableViewWithDbObjects::customMenu(QMenu &menu) const { if (this->m_menus.testFlag(CViewBaseNonTemplate::MenuOrderable) && this->hasSelection()) { const int maxOrder = this->rowCount() - 1; QMenu *menuOrder = menu.addMenu(CIcons::arrowMediumEast16(), "Order"); QLineEdit *leOrder = new QLineEdit(&menu); leOrder->setPlaceholderText("New order 0-" + QString::number(maxOrder)); const QIntValidator *v = new QIntValidator(0, maxOrder, leOrder); leOrder->setValidator(v); QWidgetAction *orderAction = new QWidgetAction(&menu); orderAction->setDefaultWidget(leOrder); menuOrder->addAction(orderAction); QObject::connect(leOrder, &QLineEdit::returnPressed, this, &COrderableViewWithDbObjects::ps_orderToLineEdit); menuOrder->addAction(CIcons::arrowMediumNorth16(), "To top", this, &COrderableViewWithDbObjects::ps_orderToTop); menuOrder->addAction(CIcons::arrowMediumSouth16(), "To bottom", this, &COrderableViewWithDbObjects::ps_orderToBottom); menuOrder->addAction(CIcons::arrowMediumWest16(), "Freeze current order", this, &COrderableViewWithDbObjects::ps_freezeCurrentOrder); menu.addSeparator(); } CViewWithDbObjects::customMenu(menu); } template void COrderableViewWithDbObjects::moveSelectedItems(int order) { if (this->isEmpty()) { return; } const ContainerType objs(this->selectedObjects()); if (objs.isEmpty()) { return; } this->m_model->moveItems(objs, order); } template void COrderableViewWithDbObjects::ps_orderToTop() { this->moveSelectedItems(0); } template void COrderableViewWithDbObjects::ps_orderToBottom() { int c = this->model()->rowCount() - 1; if (c >= 0) { this->moveSelectedItems(c); } } template void COrderableViewWithDbObjects::ps_orderToLineEdit() { if (this->isEmpty()) { return; } QLineEdit *le = qobject_cast(QObject::sender()); if (!le || le->text().isEmpty()) { return; } const int order = le->text().toInt(); this->moveSelectedItems(order); } template void COrderableViewWithDbObjects::ps_freezeCurrentOrder() { ContainerType objects = this->container(); objects.freezeOrder(); this->updateContainerAsync(objects, false); } // see here for the reason of thess forward instantiations // http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html template class CViewWithDbObjects; template class CViewWithDbObjects; template class CViewWithDbObjects; template class CViewWithDbObjects; template class CViewWithDbObjects; template class CViewWithDbObjects; template class COrderableViewWithDbObjects; template class COrderableViewWithDbObjects; } // namespace } // namespace