Added Library component
[dashstudio.git] / src / components / colorpalette / cellscolor.cpp
blob051e236f12ff99437541802601ab585476b396e3
1 /***************************************************************************
2 * Copyright (C) 2005 by Jorge Cuadrado *
3 * kuadrosxx@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
22 #include "cellscolor.h"
23 #include <dcore/debug.h>
24 #include "palettedocument.h"
26 #include <QFile>
27 #include <QDragEnterEvent>
28 #include <QMouseEvent>
29 #include <QDrag>
30 #include <QApplication>
31 #include <QPainter>
33 namespace Components {
35 struct CellsColor::Private
37 CellsColor::Type type;
38 QString name;
39 bool readOnly;
40 QPoint startDragPosition;
43 CellsColor::CellsColor(QWidget *parent, Type type)
44 : DGui::CellView(12, parent), d(new Private)
46 d->type = type;
47 d->readOnly = false;
48 setAcceptDrops(true);
52 CellsColor::~CellsColor()
54 delete d;
58 void CellsColor::setReadOnly(bool enable)
60 d->readOnly = enable;
63 bool CellsColor::isReadOnly()
65 return d->readOnly;
68 void CellsColor::setType(Type type)
70 d->type = type;
73 int CellsColor::type()
75 return d->type;
78 QString CellsColor::name() const
80 return d->name;
83 void CellsColor::setName(const QString& name)
85 d->name = name;
88 void CellsColor::save( const QString &path)
90 QFile save(path);
91 Components::PaletteDocument document(d->name, true);
93 for(int i = 0; i < columnCount() ; i++)
95 for (int j = 0; j < rowCount() ; j++)
97 QTableWidgetItem *tmpItem = itemAt(i*25, j*25);
98 if(tmpItem)
100 if(tmpItem->background().gradient())
102 document.addGradient(*tmpItem->background().gradient());
104 else if(tmpItem->background().color().isValid())
106 document.addColor(tmpItem->background().color());
111 if ( save.open(QIODevice::WriteOnly | QIODevice::Text))
113 QTextStream out(&save);
114 out << document.toString();
115 save.close();
119 void CellsColor::dragEnterEvent( QDragEnterEvent *event )
121 setFocus();
123 if (event->mimeData()->hasColor())
125 if (event->source() == this)
127 event->setDropAction(Qt::MoveAction);
128 event->accept();
130 else
132 event->acceptProposedAction();
135 else
137 event->ignore();
141 void CellsColor::dropEvent( QDropEvent *event )
143 if (event->mimeData()->hasColor())
145 QColor color = qvariant_cast<QColor>(event->mimeData()->colorData());
147 // TODO: crear item in cellscolor.cpp
149 if (event->source() == this)
151 event->setDropAction(Qt::MoveAction);
152 event->accept();
154 else
156 event->acceptProposedAction();
159 else
161 event->ignore();
165 void CellsColor::mousePressEvent(QMouseEvent* e)
167 DGui::CellView::mousePressEvent(e);
168 d->startDragPosition = e->pos();
172 void CellsColor::mouseMoveEvent(QMouseEvent* e)
174 DGui::CellView::mouseMoveEvent(e);
176 if ((e->pos() - d->startDragPosition).manhattanLength() < QApplication::startDragDistance() || !currentItem() )
177 return;
179 QDrag *drag = new QDrag( this );
180 QPixmap pix( 25, 25 );
181 QColor color= currentItem()->background().color();
182 pix.fill( color);
184 QPainter painter( &pix );
185 painter.drawRect( 0, 0, pix.width(), pix.height() );
186 painter.end();
188 QMimeData *mimeData = new QMimeData;
189 mimeData->setColorData(currentItem()->background().color());
191 drag->setMimeData(mimeData);
192 drag->setPixmap( pix );
194 /*Qt::DropAction dropAction = */drag->start(Qt::MoveAction);