closes #4: Favorite buttons has been extended with category buttons.
This commit is contained in:
@@ -40,7 +40,7 @@ add_library(commodity SHARED
|
||||
data/commoditytypedata.h
|
||||
settings/commoditysettings.cpp
|
||||
settings/commoditysettings.h
|
||||
)
|
||||
coloritemdelegate.cpp coloritemdelegate.h)
|
||||
|
||||
target_compile_definitions(commodity PRIVATE -DCOMMODITY_LIBRARY)
|
||||
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
//
|
||||
// Created by Josef Rokos on 03.05.2023.
|
||||
//
|
||||
|
||||
#include "coloritemdelegate.h"
|
||||
#include <QPainter>
|
||||
#include <QApplication>
|
||||
#include <QPaintEvent>
|
||||
#include <QHBoxLayout>
|
||||
#include <QColorDialog>
|
||||
|
||||
ColorLabel::ColorLabel(QWidget* parent) : QWidget(parent), m_color(Qt::white) {
|
||||
setAttribute(Qt::WA_StaticContents);
|
||||
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
|
||||
setFocusPolicy(Qt::NoFocus);
|
||||
}
|
||||
|
||||
QColor ColorLabel::color() const {
|
||||
return m_color;
|
||||
}
|
||||
|
||||
void ColorLabel::setColor(const QColor& color) {
|
||||
m_color = color;
|
||||
}
|
||||
|
||||
QSize ColorLabel::sizeHint() const {
|
||||
return {20,20};
|
||||
}
|
||||
|
||||
void ColorLabel::paintEvent(QPaintEvent* event) {
|
||||
QPainter painter(this);
|
||||
|
||||
QStyle* style = QApplication::style();
|
||||
painter.save();
|
||||
painter.setBrush(m_color);
|
||||
|
||||
bool dark = false;
|
||||
qreal darkness = 1-(0.299*m_color.red() + 0.587*m_color.green() + 0.114*m_color.blue())/255;
|
||||
if(darkness >= 0.5){
|
||||
dark = true;
|
||||
}
|
||||
|
||||
QColor penColor = dark ? Qt::transparent : Qt::darkGray;
|
||||
|
||||
painter.setPen(penColor);
|
||||
int border = (event->rect().height() - style->pixelMetric(QStyle::PM_IndicatorWidth))/2;
|
||||
|
||||
QRect rect(event->rect().x()+border, event->rect().y()+border,
|
||||
style->pixelMetric(QStyle::PM_IndicatorWidth),
|
||||
style->pixelMetric(QStyle::PM_IndicatorWidth));// = option.rect.adjusted(4,4,-4,-6);
|
||||
|
||||
painter.drawRect(rect);
|
||||
painter.restore();
|
||||
}
|
||||
|
||||
ColorItemEditor::ColorItemEditor(QWidget* parent) : QWidget(parent), m_buttonPressed(false)
|
||||
{
|
||||
m_colorIndicator = new ColorLabel(this);
|
||||
m_colorIndicator->setColor(m_color);
|
||||
m_button = new QToolButton(this);
|
||||
m_button->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
|
||||
m_button->setText("...");
|
||||
m_button->installEventFilter(this);
|
||||
QHBoxLayout* layout = new QHBoxLayout(this);
|
||||
layout->addWidget(m_colorIndicator);
|
||||
layout->addWidget(m_button);
|
||||
layout->setSpacing(0);
|
||||
layout->setContentsMargins(1,1,1,1);
|
||||
setFocusProxy(m_button);
|
||||
setAutoFillBackground(true);
|
||||
setLayout(layout);
|
||||
setAutoFillBackground(true);
|
||||
connect(m_button,SIGNAL(clicked()),this,SLOT(slotClicked()));
|
||||
}
|
||||
|
||||
void ColorItemEditor::setColor(const QColor& value) {
|
||||
m_color = value;
|
||||
m_colorIndicator->setColor(value);
|
||||
}
|
||||
|
||||
bool ColorItemEditor::eventFilter(QObject* obj, QEvent* event) {
|
||||
if (obj == m_button){
|
||||
if (event->type() == QEvent::FocusOut && !m_buttonPressed){
|
||||
auto focusEvent = dynamic_cast<QFocusEvent*>(event);
|
||||
if (focusEvent && focusEvent->reason()!=Qt::MouseFocusReason){
|
||||
setFocusToParent();
|
||||
emit(editingFinished());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ColorItemEditor::setFocusToParent() {
|
||||
if (parentWidget()) {
|
||||
parentWidget()->setFocus();
|
||||
}
|
||||
}
|
||||
|
||||
void ColorItemEditor::slotClicked() {
|
||||
m_buttonPressed = true;
|
||||
auto dialog = new QColorDialog(this);
|
||||
dialog->setCurrentColor(m_color);
|
||||
|
||||
if (dialog->exec()) {
|
||||
setColor(dialog->currentColor());
|
||||
}
|
||||
|
||||
delete dialog;
|
||||
setFocusToParent();
|
||||
emit(editingFinished());
|
||||
}
|
||||
|
||||
ColorItemDelegate::ColorItemDelegate(QObject* parent) : QStyledItemDelegate(parent) {
|
||||
|
||||
}
|
||||
|
||||
void ColorItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
|
||||
QColor color(index.data().toString());
|
||||
|
||||
if (color.isValid()) {
|
||||
painter->save();
|
||||
painter->setBrush(color);
|
||||
|
||||
QRect rect(option.rect.x(), option.rect.y(), option.rect.width(), option.rect.height());
|
||||
painter->drawRect(rect);
|
||||
painter->restore();
|
||||
} else {
|
||||
QStyledItemDelegate::paint(painter, option, index);
|
||||
}
|
||||
}
|
||||
|
||||
QSize ColorItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const {
|
||||
return QStyledItemDelegate::sizeHint(option, index);
|
||||
}
|
||||
|
||||
QWidget* ColorItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {
|
||||
QColor color(index.data().toString());
|
||||
if (!color.isValid()) {
|
||||
color = QColorConstants::Gray;
|
||||
}
|
||||
|
||||
auto editor = new ColorItemEditor(parent);
|
||||
editor->setColor(color);
|
||||
connect(editor, &ColorItemEditor::editingFinished, this, &ColorItemDelegate::commitAndCloseEditor);
|
||||
return editor;
|
||||
}
|
||||
|
||||
void ColorItemDelegate::commitAndCloseEditor() {
|
||||
auto editor = qobject_cast<ColorItemEditor*>(sender());
|
||||
emit commitData(editor);
|
||||
emit closeEditor(editor);
|
||||
}
|
||||
|
||||
void ColorItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const {
|
||||
auto edit = qobject_cast<ColorItemEditor*>(editor);
|
||||
QColor color(index.data().toString());
|
||||
|
||||
if (edit && color.isValid()) {
|
||||
edit->setColor(color);
|
||||
} else {
|
||||
QStyledItemDelegate::setEditorData(editor, index);
|
||||
}
|
||||
}
|
||||
|
||||
void ColorItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const {
|
||||
auto edit = qobject_cast<ColorItemEditor*>(editor);
|
||||
|
||||
if (edit) {
|
||||
model->setData(index, edit->color().name());
|
||||
} else {
|
||||
QStyledItemDelegate::setModelData(editor, model, index);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
//
|
||||
// Created by Josef Rokos on 03.05.2023.
|
||||
//
|
||||
|
||||
#ifndef COLORITEMDELEGATE_H
|
||||
#define COLORITEMDELEGATE_H
|
||||
|
||||
#include <QStyledItemDelegate>
|
||||
#include <QToolButton>
|
||||
|
||||
class ColorItemDelegate : public QStyledItemDelegate {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ColorItemDelegate(QObject* parent = nullptr);
|
||||
~ColorItemDelegate() override = default;
|
||||
|
||||
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
||||
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
||||
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
||||
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
|
||||
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
|
||||
|
||||
private slots:
|
||||
void commitAndCloseEditor();
|
||||
|
||||
|
||||
};
|
||||
|
||||
class ColorLabel : public QWidget{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ColorLabel(QWidget* parent = nullptr);
|
||||
QColor color() const;
|
||||
void setColor(const QColor &color);
|
||||
QSize sizeHint() const override;
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *event) override;
|
||||
private:
|
||||
QColor m_color;
|
||||
};
|
||||
|
||||
class ColorItemEditor : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ColorItemEditor(QWidget *parent = nullptr);
|
||||
QColor color(){return m_color;}
|
||||
void setColor(const QColor& value);
|
||||
protected:
|
||||
bool eventFilter(QObject *obj, QEvent *event) override;
|
||||
private:
|
||||
void setFocusToParent();
|
||||
signals:
|
||||
void editingFinished();
|
||||
private slots:
|
||||
void slotClicked();
|
||||
private:
|
||||
QColor m_color;
|
||||
QToolButton* m_button;
|
||||
ColorLabel* m_colorIndicator;
|
||||
bool m_buttonPressed;
|
||||
};
|
||||
|
||||
#endif //COLORITEMDELEGATE_H
|
||||
@@ -8,7 +8,7 @@
|
||||
"default" : "",
|
||||
"CZ" : ""
|
||||
},
|
||||
"schemaVersion" : 1,
|
||||
"schemaVersion" : 2,
|
||||
"sql" : [
|
||||
"CREATE TABLE \"CommodityTypeData\" (
|
||||
\"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
@@ -26,8 +26,13 @@ CREATE TABLE \"CommodityData\" (
|
||||
CONSTRAINT \"type_fk\"
|
||||
FOREIGN KEY (\"type\")
|
||||
REFERENCES \"CommodityTypeData\" (\"id\")
|
||||
DEFERRABLE INITIALLY DEFERRED);"
|
||||
|
||||
DEFERRABLE INITIALLY DEFERRED);
|
||||
",
|
||||
"
|
||||
ALTER TABLE \"CommodityTypeData\" ADD \"color\" TEXT NULL;
|
||||
ALTER TABLE \"CommodityData\" ADD \"favorite\" INTEGER NULL;
|
||||
UPDATE \"CommodityData\" SET \"favorite\"=0;
|
||||
"
|
||||
],
|
||||
"dependencies" : [ "SHOP" ],
|
||||
"translations" : {
|
||||
|
||||
@@ -25,6 +25,7 @@ CommodityForm::CommodityForm(QWidget *parent) :
|
||||
<< ComboData(Enums::SECOND_LOWER,tr("Second Lower"));
|
||||
registerBinding(ui->vat, vt);
|
||||
registerBinding(ui->count);
|
||||
registerBinding(ui->favorite);
|
||||
|
||||
m_codeAsNumber = false;
|
||||
}
|
||||
|
||||
@@ -94,6 +94,13 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="7" column="1">
|
||||
<widget class="QCheckBox" name="favorite">
|
||||
<property name="text">
|
||||
<string>Favorite</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<tabstops>
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
#include "commodityservice.h"
|
||||
|
||||
QList<QSharedPointer<IShopItem> > CommodityService::shopItems()
|
||||
QList<QSharedPointer<IShopItem> > CommodityService::shopItems(const QString& category/* = ""*/)
|
||||
{
|
||||
QList<QSharedPointer<IShopItem> > ret;
|
||||
|
||||
foreach (QSharedPointer<CommodityData> data, all()) {
|
||||
ret.append(qSharedPointerDynamicCast<IShopItem, CommodityData>(data));
|
||||
if (category.isEmpty() || (data->favorite() && data->category() == category)) {
|
||||
ret.append(qSharedPointerDynamicCast<IShopItem, CommodityData>(data));
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
||||
@@ -12,7 +12,7 @@ public:
|
||||
|
||||
// ISellableService interface
|
||||
public:
|
||||
QList<IShopItemPtr> shopItems() override;
|
||||
QList<IShopItemPtr> shopItems(const QString& category = "") override;
|
||||
void addedToVoucher(int itemId, int countAdded) override;
|
||||
IShopItemPtr shopItem(int itemId) override;
|
||||
ISeller *seller() override;
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include <service.h>
|
||||
#include <settingsservice.h>
|
||||
|
||||
#include "coloritemdelegate.h"
|
||||
|
||||
CommoditySettingsForm::CommoditySettingsForm(QWidget *parent) :
|
||||
FormBinder<CommoditySettings>(parent),
|
||||
ui(new Ui::CommoditySettingsForm)
|
||||
@@ -13,9 +15,10 @@ CommoditySettingsForm::CommoditySettingsForm(QWidget *parent) :
|
||||
registerBinding(ui->codeAsNumber);
|
||||
|
||||
m_table = new AutoTableModel<CommodityTypeData>();
|
||||
m_table->setEditableCols(QList<int>() << 0);
|
||||
m_table->setEditableCols(QList<int>() << 0 << 1);
|
||||
ui->commodityTypesTable->setModel(m_table);
|
||||
ui->commodityTypesTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
|
||||
ui->commodityTypesTable->setItemDelegateForColumn(1, new ColorItemDelegate(this));
|
||||
}
|
||||
|
||||
CommoditySettingsForm::~CommoditySettingsForm()
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace qx {
|
||||
t.data(&CommodityData::m_price, "price");
|
||||
t.data(&CommodityData::m_vat, "vat");
|
||||
t.data(&CommodityData::m_count, "count");
|
||||
t.data(&CommodityData::m_favorite, "favorite");
|
||||
|
||||
t.relationManyToOne(&CommodityData::m_type, "type");
|
||||
}
|
||||
@@ -25,7 +26,7 @@ CommodityData::CommodityData(QObject *parent)
|
||||
m_price = 0;
|
||||
m_vat = Enums::NONE;
|
||||
}
|
||||
long CommodityData::id()
|
||||
long CommodityData::id() const
|
||||
{
|
||||
return m_id;
|
||||
}
|
||||
@@ -114,7 +115,7 @@ Enums::VatType CommodityData::vatType()
|
||||
return vat();
|
||||
}
|
||||
|
||||
QString CommodityData::pluginId()
|
||||
QString CommodityData::pluginId() const
|
||||
{
|
||||
return "COMMODITY";
|
||||
}
|
||||
@@ -123,6 +124,22 @@ QStringList CommodityData::eagerLoad() {
|
||||
return { "type" };
|
||||
}
|
||||
|
||||
bool CommodityData::favorite() {
|
||||
return m_favorite;
|
||||
}
|
||||
|
||||
void CommodityData::setFavorite(bool favorite) {
|
||||
m_favorite = favorite;
|
||||
}
|
||||
|
||||
QString CommodityData::color() {
|
||||
return m_type->color();
|
||||
}
|
||||
|
||||
QString CommodityData::category() {
|
||||
return m_type->name();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -24,11 +24,12 @@ class CommodityData : public IShopItem
|
||||
Q_PROPERTY(QDecDouble price READ price WRITE setPrice)
|
||||
Q_PROPERTY(Enums::VatType vat READ vat WRITE setVat)
|
||||
Q_PROPERTY(int count READ count WRITE setCount)
|
||||
Q_PROPERTY(bool favorite READ favorite WRITE setFavorite)
|
||||
|
||||
public:
|
||||
explicit CommodityData(QObject *parent = nullptr);
|
||||
|
||||
long id() override;
|
||||
long id() const override;
|
||||
void setId(long id);
|
||||
|
||||
QString name() override;
|
||||
@@ -52,6 +53,9 @@ public:
|
||||
int count() const;
|
||||
void setCount(int count);
|
||||
|
||||
bool favorite() override;
|
||||
void setFavorite(bool favorite);
|
||||
|
||||
Q_INVOKABLE QStringList eagerLoad();
|
||||
|
||||
private:
|
||||
@@ -63,13 +67,15 @@ private:
|
||||
int m_price;
|
||||
Enums::VatType m_vat;
|
||||
int m_count;
|
||||
bool m_favorite;
|
||||
|
||||
// IShopItem interface
|
||||
public:
|
||||
QDecDouble unitPrice() override;
|
||||
Enums::VatType vatType() override;
|
||||
QString pluginId() override;
|
||||
|
||||
QString pluginId() const override;
|
||||
QString color() override;
|
||||
QString category() override;
|
||||
};
|
||||
|
||||
typedef QSharedPointer<CommodityData> CommodityDataPtr;
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace qx {
|
||||
t.setName("CommodityTypeData");
|
||||
t.id(&CommodityTypeData::m_id, "id");
|
||||
t.data(&CommodityTypeData::m_name, "name");
|
||||
t.data(&CommodityTypeData::m_color, "color");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,4 +48,12 @@ QString CommodityTypeData::toString()
|
||||
return this->name();
|
||||
}
|
||||
|
||||
QString CommodityTypeData::color() const {
|
||||
return m_color;
|
||||
}
|
||||
|
||||
void CommodityTypeData::setColor(const QString& color) {
|
||||
m_color = color;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ class CommodityTypeData :public ComboItem
|
||||
|
||||
QX_REGISTER_FRIEND_CLASS(CommodityTypeData)
|
||||
Q_PROPERTY(QString name READ name WRITE setName)
|
||||
Q_PROPERTY(QString color READ color WRITE setColor)
|
||||
|
||||
public:
|
||||
explicit CommodityTypeData(QObject *parent = nullptr);
|
||||
@@ -22,9 +23,13 @@ public:
|
||||
QString name() const;
|
||||
void setName(const QString &name);
|
||||
|
||||
QString color() const;
|
||||
void setColor(const QString& color);
|
||||
|
||||
private:
|
||||
long m_id{0};
|
||||
QString m_name;
|
||||
QString m_color;
|
||||
|
||||
// ComboItem interface
|
||||
public:
|
||||
|
||||
Reference in New Issue
Block a user