refs #452 distributor selector component UI widget

This commit is contained in:
Klaus Basan
2015-09-24 00:07:32 +02:00
committed by Mathew Sutcliffe
parent 3bbaecb0d8
commit 2143b20ae5
3 changed files with 352 additions and 0 deletions

View File

@@ -0,0 +1,188 @@
/* 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 "dbdistributorselectorcomponent.h"
#include "ui_dbdistributorselectorcomponent.h"
#include "blackgui/guiutility.h"
#include <QMimeData>
using namespace BlackGui;
using namespace BlackMisc;
using namespace BlackMisc::Simulation;
using namespace BlackMisc::Network;
namespace BlackGui
{
namespace Components
{
CDbDistributorSelectorComponent::CDbDistributorSelectorComponent(QWidget *parent) :
QFrame(parent),
ui(new Ui::CDbDistributorSelectorComponent)
{
ui->setupUi(this);
this->setAcceptDrops(true);
this->setAcceptedMetaTypeIds({qMetaTypeId<CDistributor>(), qMetaTypeId<CDistributorList>()});
connect(ui->le_Distributor, &QLineEdit::returnPressed, this, &CDbDistributorSelectorComponent::ps_dataChanged);
connect(ui->le_Distributor, &QLineEdit::returnPressed, this, &CDbDistributorSelectorComponent::ps_dataChanged);
}
CDbDistributorSelectorComponent::~CDbDistributorSelectorComponent()
{
gracefulShutdown();
}
void CDbDistributorSelectorComponent::setProvider(Network::IWebDataServicesProvider *webDataReaderProvider)
{
CWebDataServicesAware::setProvider(webDataReaderProvider);
connectSwiftDatabaseSignals(
this,
std::bind(&CDbDistributorSelectorComponent::ps_distributorsRead, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)
);
int c = getDistributorsCount();
if (c > 0)
{
ps_distributorsRead(CDbFlags::DistributorEntity, CDbFlags::ReadFinished, c);
}
}
void CDbDistributorSelectorComponent::setDistributor(const CDistributor &distributor)
{
this->ui->le_Distributor->setText(distributor.getId());
ui->lbl_Description->setText(distributor.getDescription());
if (distributor != m_currentDistributor)
{
m_currentDistributor = distributor;
emit changedDistributor(distributor);
}
}
void CDbDistributorSelectorComponent::setDistributor(const QString &distributorKey)
{
QString key(distributorKey.toUpper().trimmed());
CDistributor d(getDistributors().findByKey(key));
ui->lbl_Description->setText("");
if (d.hasCompleteData())
{
this->setDistributor(d);
}
else
{
this->ui->le_Distributor->setText(key);
}
}
CDistributor CDbDistributorSelectorComponent::getDistributor() const
{
if (!hasProvider()) { return CDistributor(); }
QString distributorKey(this->ui->le_Distributor->text().trimmed().toUpper());
CDistributor d(getDistributors().findByKey(distributorKey));
return d;
}
void CDbDistributorSelectorComponent::setReadOnly(bool readOnly)
{
this->ui->le_Distributor->setReadOnly(readOnly);
}
void CDbDistributorSelectorComponent::withDistributorDescription(bool description)
{
this->ui->lbl_Description->setVisible(description);
}
bool CDbDistributorSelectorComponent::isSet() const
{
return this->getDistributor().hasCompleteData();
}
void CDbDistributorSelectorComponent::clear()
{
this->ui->le_Distributor->clear();
}
void CDbDistributorSelectorComponent::dragEnterEvent(QDragEnterEvent *event)
{
if (!event || !acceptDrop(event->mimeData())) { return; }
setBackgroundRole(QPalette::Highlight);
event->acceptProposedAction();
}
void CDbDistributorSelectorComponent::dragMoveEvent(QDragMoveEvent *event)
{
if (!event || !acceptDrop(event->mimeData())) { return; }
event->acceptProposedAction();
}
void CDbDistributorSelectorComponent::dragLeaveEvent(QDragLeaveEvent *event)
{
if (!event) { return; }
event->accept();
}
void CDbDistributorSelectorComponent::dropEvent(QDropEvent *event)
{
if (!event || !acceptDrop(event->mimeData())) { return; }
CVariant valueVariant(toCVariant(event->mimeData()));
if (valueVariant.isValid())
{
if (valueVariant.canConvert<CDistributor>())
{
CDistributor distributor(valueVariant.value<CDistributor>());
if (!distributor.hasValidDbKey()) { return; }
this->setDistributor(distributor);
}
else if (valueVariant.canConvert<CDistributorList>())
{
CDistributorList distributors(valueVariant.value<CDistributorList>());
if (distributors.isEmpty()) { return; }
this->setDistributor(distributors.front());
}
}
}
void CDbDistributorSelectorComponent::ps_distributorsRead(CDbFlags::Entity entity, CDbFlags::ReadState readState, int count)
{
if (!hasProvider()) { return; }
if (entity.testFlag(CDbFlags::DistributorEntity) && readState == CDbFlags::ReadFinished)
{
if (count > 0)
{
QCompleter *c = new QCompleter(this->getDistributors().toDbKeyList(), this);
c->setCaseSensitivity(Qt::CaseInsensitive);
c->setCompletionMode(QCompleter::PopupCompletion);
c->setMaxVisibleItems(10);
this->connect(c, static_cast<void (QCompleter::*)(const QString &)>(&QCompleter::activated), this, &CDbDistributorSelectorComponent::ps_completerActivated);
this->ui->le_Distributor->setCompleter(c);
m_completerDistributorNames.reset(c); // deletes any old completer
this->setReadOnly(false);
}
else
{
this->m_completerDistributorNames.reset(nullptr);
this->setReadOnly(true);
}
}
}
void CDbDistributorSelectorComponent::ps_dataChanged()
{
if (!hasProvider()) { return; }
QString key(this->ui->le_Distributor->text().trimmed().toUpper());
CDistributor d(this->getDistributors().findByKey(key));
this->setDistributor(d);
}
void CDbDistributorSelectorComponent::ps_completerActivated(const QString &distributorKey)
{
this->setDistributor(distributorKey);
}
} // ns
} // ns

View File

@@ -0,0 +1,105 @@
/* 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.
*/
//! \file
#ifndef BLACKGUI_DBDISTRIBUTORSELECTORCOMPONENT_H
#define BLACKGUI_DBDISTRIBUTORSELECTORCOMPONENT_H
#include "blackgui/blackguiexport.h"
#include "blackcore/webdataservices.h"
#include "blackgui/dropbase.h"
#include "blackmisc/simulation/distributor.h"
#include <QFrame>
#include <QScopedPointer>
#include <QCompleter>
namespace Ui { class CDbDistributorSelectorComponent; }
namespace BlackGui
{
namespace Components
{
/*!
* Distributor selector
*/
class BLACKGUI_EXPORT CDbDistributorSelectorComponent :
public QFrame,
public BlackMisc::Network::CWebDataServicesAware,
public BlackGui::CDropBase
{
Q_OBJECT
public:
//! Constructor
explicit CDbDistributorSelectorComponent(QWidget *parent = nullptr);
//! Destructor
~CDbDistributorSelectorComponent();
//! \copydoc CWebDataReaderAware::setProvider
virtual void setProvider(BlackMisc::Network::IWebDataServicesProvider *webDataReaderProvider) override;
//! Current distributor
void setDistributor(const BlackMisc::Simulation::CDistributor &distributor);
//! Current Distributor
void setDistributor(const QString &distributorKey);
//! Distributor
BlackMisc::Simulation::CDistributor getDistributor() const;
//! Read only
void setReadOnly(bool readOnly);
//! Display distributor description
void withDistributorDescription(bool description);
//! Set with valid Distributor
bool isSet() const;
//! Clear selection
void clear();
signals:
//! Distributor was changed
void changedDistributor(const BlackMisc::Simulation::CDistributor &distributor);
protected:
//! \copydoc QWidget::dragEnterEvent
virtual void dragEnterEvent(QDragEnterEvent *event) override;
//! \copydoc QWidget::dragMoveEvent
virtual void dragMoveEvent(QDragMoveEvent *event) override;
//! \copydoc QWidget::dragLeaveEvent
virtual void dragLeaveEvent(QDragLeaveEvent *event) override;
//! \copydoc QWidget::dropEvent
virtual void dropEvent(QDropEvent *event) override;
private slots:
//! Distributors have been read
void ps_distributorsRead(BlackMisc::Network::CDbFlags::Entity entity, BlackMisc::Network::CDbFlags::ReadState readState, int count);
//! Data have been changed
void ps_dataChanged();
//! Data have been changed
void ps_completerActivated(const QString &distributorKey);
private:
QScopedPointer<Ui::CDbDistributorSelectorComponent> ui;
QScopedPointer<QCompleter> m_completerDistributorNames;
QMetaObject::Connection m_signalConnection;
BlackMisc::Simulation::CDistributor m_currentDistributor;
};
}
}
#endif // guard

View File

@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CDbDistributorSelectorComponent</class>
<widget class="QFrame" name="CDbDistributorSelectorComponent">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>191</width>
<height>22</height>
</rect>
</property>
<property name="windowTitle">
<string>Frame</string>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QGridLayout" name="gl_DistributorSelectorComponent">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QLineEdit" name="le_Distributor">
<property name="placeholderText">
<string>Distributor key</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="lbl_Description">
<property name="minimumSize">
<size>
<width>50</width>
<height>0</height>
</size>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>