You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
3.7 KiB
C++
130 lines
3.7 KiB
C++
7 years ago
|
/*
|
||
|
* Copyright 2017-2018 Michail Vourlakos <mvourlakos@gmail.com>
|
||
|
*
|
||
|
* This file is part of Latte-Dock
|
||
|
*
|
||
|
* Latte-Dock is free software; you can redistribute it and/or
|
||
|
* modify it under the terms of the GNU General Public License as
|
||
|
* published by the Free Software Foundation; either version 2 of
|
||
|
* the License, or (at your option) any later version.
|
||
|
*
|
||
|
* Latte-Dock is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
5 years ago
|
#include "backgroundcmbdelegate.h"
|
||
8 years ago
|
|
||
6 years ago
|
// local
|
||
5 years ago
|
#include "backgroundcmbitemdelegate.h"
|
||
7 years ago
|
|
||
6 years ago
|
// Qt
|
||
8 years ago
|
#include <QComboBox>
|
||
|
#include <QDebug>
|
||
5 years ago
|
#include <QFileInfo>
|
||
8 years ago
|
#include <QWidget>
|
||
|
#include <QModelIndex>
|
||
|
#include <QPainter>
|
||
|
#include <QString>
|
||
|
|
||
6 years ago
|
// KDE
|
||
7 years ago
|
#include <KLocalizedString>
|
||
|
|
||
5 years ago
|
namespace Latte {
|
||
|
namespace Settings {
|
||
|
namespace Layouts {
|
||
|
namespace Delegates {
|
||
|
|
||
5 years ago
|
BackgroundCmbBox::BackgroundCmbBox(QObject *parent, QString iconsPath, QStringList colors)
|
||
8 years ago
|
: QItemDelegate(parent),
|
||
8 years ago
|
m_iconsPath(iconsPath),
|
||
|
Colors(colors)
|
||
8 years ago
|
{
|
||
|
}
|
||
|
|
||
5 years ago
|
QWidget *BackgroundCmbBox::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||
8 years ago
|
{
|
||
|
QComboBox *editor = new QComboBox(parent);
|
||
5 years ago
|
editor->setItemDelegate(new BackgroundCmbBoxItem(editor, m_iconsPath));
|
||
8 years ago
|
|
||
8 years ago
|
for (unsigned int i = 0; i < Colors.count(); ++i) {
|
||
8 years ago
|
if (Colors[i] != "sepia") {
|
||
|
QPixmap pixmap(50, 50);
|
||
|
pixmap.fill(QColor(Colors[i]));
|
||
|
QIcon icon(pixmap);
|
||
|
|
||
|
editor->addItem(icon, Colors[i]);
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
QString value = index.model()->data(index, Qt::BackgroundRole).toString();
|
||
|
|
||
7 years ago
|
const QModelIndex &indexOriginal = index;
|
||
|
|
||
7 years ago
|
//! add the background if exists
|
||
|
if (value.startsWith("/")) {
|
||
|
QIcon icon(value);
|
||
|
editor->addItem(icon, value);
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
8 years ago
|
return editor;
|
||
|
}
|
||
|
|
||
5 years ago
|
void BackgroundCmbBox::setEditorData(QWidget *editor, const QModelIndex &index) const
|
||
8 years ago
|
{
|
||
|
QComboBox *comboBox = static_cast<QComboBox *>(editor);
|
||
8 years ago
|
QString value = index.model()->data(index, Qt::BackgroundRole).toString();
|
||
7 years ago
|
|
||
|
int pos = Colors.indexOf(value);
|
||
|
|
||
|
if (pos == -1 && value.startsWith("/")) {
|
||
7 years ago
|
comboBox->setCurrentIndex(Colors.count());
|
||
7 years ago
|
} else {
|
||
|
comboBox->setCurrentIndex(Colors.indexOf(value));
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
void BackgroundCmbBox::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
|
||
8 years ago
|
{
|
||
|
QComboBox *comboBox = static_cast<QComboBox *>(editor);
|
||
7 years ago
|
|
||
7 years ago
|
QString itemData = comboBox->currentData().toString();
|
||
5 years ago
|
model->setData(index, comboBox->currentText(), Qt::BackgroundRole);
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
void BackgroundCmbBox::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||
8 years ago
|
{
|
||
|
editor->setGeometry(option.rect);
|
||
|
}
|
||
|
|
||
5 years ago
|
void BackgroundCmbBox::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||
8 years ago
|
{
|
||
|
QStyleOptionViewItem myOption = option;
|
||
5 years ago
|
QVariant background = index.data(Qt::BackgroundRole);
|
||
7 years ago
|
|
||
5 years ago
|
if (background.isValid()) {
|
||
|
QString backgroundStr = background.toString();
|
||
8 years ago
|
|
||
5 years ago
|
QString colorPath = backgroundStr.startsWith("/") ? backgroundStr : m_iconsPath + backgroundStr + "print.jpg";
|
||
8 years ago
|
|
||
7 years ago
|
if (QFileInfo(colorPath).exists()) {
|
||
|
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()));
|
||
|
}
|
||
8 years ago
|
}
|
||
5 years ago
|
}
|
||
8 years ago
|
|
||
5 years ago
|
}
|
||
|
}
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|