You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

132 lines
3.5 KiB

/*
* This file is part of kmail
*
* Copyright (C) 2000 Espen Sand, espen@kde.org
* Copyright (C) 2007 Mathias Soeken, msoeken@tzi.de
*
* This program 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.
*
* This program 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "colorlistbox.h"
#include <QColorDialog>
#include <KColorMimeData>
#include <QDragEnterEvent>
#include <QDragLeaveEvent>
#include <QDragMoveEvent>
#include <QDropEvent>
#include <QHeaderView>
ColorListBox::ColorListBox(QWidget *parent)
: QTreeWidget(parent), mCurrentOnDragEnter(Q_NULLPTR)
{
setColumnCount(1);
setRootIsDecorated(false);
header()->hide();
connect(this, &ColorListBox::doubleClicked, this, &ColorListBox::newColor);
setAcceptDrops(true);
}
void ColorListBox::addColor(const QString &text, const QColor &color)
{
QTreeWidgetItem *item = new QTreeWidgetItem(QStringList() << text);
item->setData(0, Qt::DecorationRole, color);
addTopLevelItem(item);
}
void ColorListBox::setColorSilently(int index, const QColor &color)
{
if (index < model()->rowCount()) {
topLevelItem(index)->setData(0, Qt::DecorationRole, color);
}
}
void ColorListBox::setColor(int index, const QColor &color)
{
if (index < model()->rowCount()) {
topLevelItem(index)->setData(0, Qt::DecorationRole, color);
Q_EMIT changed();
}
}
QColor ColorListBox::color(int index) const
{
if (index < model()->rowCount()) {
return topLevelItem(index)->data(0, Qt::DecorationRole).value<QColor>();
} else {
return Qt::black;
}
}
void ColorListBox::newColor(const QModelIndex &index)
{
if (!isEnabled()) {
return;
}
if (index.isValid()) {
QColor c = color(index.row());
c = QColorDialog::getColor(c, this);
if (c.isValid()) {
setColor(index.row(), c);
}
}
}
void ColorListBox::dragEnterEvent(QDragEnterEvent *e)
{
if (KColorMimeData::canDecode(e->mimeData()) && isEnabled()) {
mCurrentOnDragEnter = currentItem();
e->setAccepted(true);
} else {
mCurrentOnDragEnter = Q_NULLPTR;
e->setAccepted(false);
}
}
void ColorListBox::dragLeaveEvent(QDragLeaveEvent *)
{
if (mCurrentOnDragEnter) {
setCurrentItem(mCurrentOnDragEnter);
mCurrentOnDragEnter = Q_NULLPTR;
}
}
void ColorListBox::dragMoveEvent(QDragMoveEvent *e)
{
if (KColorMimeData::canDecode(e->mimeData()) && isEnabled()) {
QTreeWidgetItem *item = itemAt(e->pos());
if (item) {
setCurrentItem(item);
}
}
}
void ColorListBox::dropEvent(QDropEvent *e)
{
const QColor color = KColorMimeData::fromMimeData(e->mimeData());
if (color.isValid()) {
QTreeWidgetItem *item = currentItem();
if (item) {
item->setData(0, Qt::DecorationRole, color);
Q_EMIT changed();
}
mCurrentOnDragEnter = Q_NULLPTR;
}
}