1
0
mirror of https://github.com/KDE/latte-dock.git synced 2025-03-06 04:58:19 +03:00

add functionality for the color combobox

--add more needed functionality for the color combobox.
set the current color correctly for the combobox.
This commit is contained in:
Michail Vourlakos 2017-07-18 17:28:31 +03:00
parent ef8e587672
commit 0762e57bab
6 changed files with 75 additions and 50 deletions

View File

@ -22,6 +22,7 @@ set(lattedock-app_SRCS
importer.cpp
layoutsDelegates/checkboxdelegate.cpp
layoutsDelegates/colorcmbboxdelegate.cpp
layoutsDelegates/colorcmbboxitemdelegate.cpp
main.cpp
)

View File

@ -69,7 +69,21 @@ LayoutConfigDialog::LayoutConfigDialog(QWidget *parent, LayoutManager *manager)
ui->layoutsView->setItemDelegateForColumn(3, new CheckBoxDelegate(this));
QString iconsPath(m_manager->corona()->kPackage().path() + "../../plasmoids/org.kde.latte.containment/contents/icons/");
ui->layoutsView->setItemDelegateForColumn(1, new ColorCmbBoxDelegate(this, iconsPath));
//!find the available colors
QDir layoutDir(iconsPath);
QStringList filter;
filter.append(QString("*print.jpg"));
QStringList files = layoutDir.entryList(filter, QDir::Files | QDir::NoSymLinks);
QStringList colors;
foreach (auto file, files) {
int colorEnd = file.lastIndexOf("print.jpg");
QString color = file.remove(colorEnd, 9);
colors.append(color);
}
ui->layoutsView->setItemDelegateForColumn(1, new ColorCmbBoxDelegate(this, iconsPath, colors));
}
LayoutConfigDialog::~LayoutConfigDialog()

View File

@ -2,59 +2,26 @@
#include <QComboBox>
#include <QDebug>
#include <QDir>
#include <QWidget>
#include <QModelIndex>
#include <QApplication>
#include <QPainter>
#include <QString>
#include <iostream>
ColorCmbBoxDelegate::ColorCmbBoxDelegate(QObject *parent, QString iconsPath)
ColorCmbBoxDelegate::ColorCmbBoxDelegate(QObject *parent, QString iconsPath, QStringList colors)
: QItemDelegate(parent),
m_iconsPath(iconsPath)
m_iconsPath(iconsPath),
Colors(colors)
{
Items.push_back("Test0");
Items.push_back("Test1");
Items.push_back("Test2");
Items.push_back("Test3");
Items.push_back("Test4");
Items.push_back("Test5");
Items.push_back("Test6");
Items.push_back("Test7");
Items.push_back("Test8");
Items.push_back("Test...");
Items.push_back("Test...");
Items.push_back("Test...");
Items.push_back("Test...");
Items.push_back("Test...");
Items.push_back("Test...");
Items.push_back("Test...");
Items.push_back("Test...");
Items.push_back("Test...");
Items.push_back("Test...");
Items.push_back("Test...");
Items.push_back("Test...");
Items.push_back("Test...");
Items.push_back("Test...");
Items.push_back("Test...");
Items.push_back("Test...");
Items.push_back("Test...");
Items.push_back("Test...");
Items.push_back("Test...");
Items.push_back("Test...");
Items.push_back("Test...");
Items.push_back("Test...");
}
QWidget *ColorCmbBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QComboBox *editor = new QComboBox(parent);
//QString colorPath = "/usr/share/plasma/plasmoids/org.kde.latte.containment/contents/icons/blueprint.jpg";
for (unsigned int i = 0; i < Items.size(); ++i) {
editor->addItem(Items[i].c_str());
for (unsigned int i = 0; i < Colors.count(); ++i) {
editor->addItem(Colors[i]);
}
return editor;
@ -63,14 +30,14 @@ QWidget *ColorCmbBoxDelegate::createEditor(QWidget *parent, const QStyleOptionVi
void ColorCmbBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QComboBox *comboBox = static_cast<QComboBox *>(editor);
int value = index.model()->data(index, Qt::EditRole).toUInt();
comboBox->setCurrentIndex(value);
QString value = index.model()->data(index, Qt::BackgroundRole).toString();
comboBox->setCurrentIndex(Colors.indexOf(value));
}
void ColorCmbBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QComboBox *comboBox = static_cast<QComboBox *>(editor);
model->setData(index, comboBox->currentIndex(), Qt::EditRole);
model->setData(index, comboBox->currentText(), Qt::BackgroundRole);
}
void ColorCmbBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
@ -81,8 +48,6 @@ void ColorCmbBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOpti
void ColorCmbBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem myOption = option;
//QString text = Items[index.row()].c_str();
//myOption.text = text;
QVariant value = index.data(Qt::BackgroundRole);
if (value.isValid()) {

View File

@ -1,9 +1,6 @@
#ifndef COLORCMBBOXDELEGATE_H
#define COLORCMBBOXDELEGATE_H
#include <string>
#include <vector>
#include <QItemDelegate>
class QModelIndex;
@ -13,7 +10,7 @@ class QVariant;
class ColorCmbBoxDelegate : public QItemDelegate {
Q_OBJECT
public:
ColorCmbBoxDelegate(QObject *parent = 0, QString iconsPath = QString());
ColorCmbBoxDelegate(QObject *parent = 0, QString iconsPath = QString(), QStringList colors = QStringList());
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
@ -22,7 +19,7 @@ public:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
private:
std::vector<std::string> Items;
QStringList Colors;
QString m_iconsPath;
};

View File

@ -0,0 +1,34 @@
#include "colorcmbboxitemdelegate.h"
#include <QApplication>
#include <QDebug>
#include <QPainter>
#include <QString>
ColorCmbBoxItemDelegate::ColorCmbBoxItemDelegate(QObject *parent)
: QAbstractItemDelegate(parent)
{
}
void ColorCmbBoxItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem myOption = option;
//QString text = Items[index.row()].c_str();
//myOption.text = text;
QVariant value = index.data(Qt::DisplayRole);
if (value.isValid()) {
qDebug() << value.toString();
/*QString colorPath = m_iconsPath + value.toString() + "print.jpg";
QBrush colorBrush;
colorBrush.setTextureImage(QImage(colorPath).scaled(QSize(50, 50)));
painter->setBrush(colorBrush);
painter->drawRect(QRect(option.rect.x(), option.rect.y(),
option.rect.width(), option.rect.height()));*/
}
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);
}

View File

@ -0,0 +1,14 @@
#ifndef COLORCMBBOXITEMDELEGATE_H
#define COLORCMBBOXITEMDELEGATE_H
#include <QAbstractItemDelegate>
class ColorCmbBoxItemDelegate : public QAbstractItemDelegate {
Q_OBJECT
public:
ColorCmbBoxItemDelegate(QObject *parent = 0);
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
#endif