- Save/load palettes properly
[dashstudio.git] / src / components / colorpalette / private / cellscolor.cpp
blobb7379d10df52b48481e627294625cd53491e8a3f
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 <QDir>
28 #include <QDragEnterEvent>
29 #include <QMouseEvent>
30 #include <QDrag>
31 #include <QApplication>
32 #include <QPainter>
34 #include "paletteparser.h"
36 namespace Component {
38 struct CellsColor::Private
40 CellsColor::Type type;
41 QString name;
42 bool readOnly;
43 QPoint startDragPosition;
45 QString fileName;
48 CellsColor::CellsColor(QWidget *parent)
49 : DGui::CellView(12, parent), d(new Private)
51 d->type = Color;
52 d->readOnly = false;
53 setAcceptDrops(true);
57 CellsColor::~CellsColor()
59 save();
60 delete d;
64 bool CellsColor::load(const QString &file)
66 PaletteParser parser;
67 QFile f(file);
69 if(parser.parse(&f))
71 QList<QBrush> brushes = parser.brushes();
73 foreach(QBrush brush, brushes)
75 addItem(brush);
78 d->name = parser.paletteName();
79 d->readOnly = !parser.isEditable();
81 d->fileName = file;
83 return true;
86 dError() << "Error while parse palette file: " << file;
87 return false;
90 void CellsColor::setReadOnly(bool enable)
92 d->readOnly = enable;
95 bool CellsColor::isReadOnly() const
97 return d->readOnly;
100 void CellsColor::setType(Type type)
102 d->type = type;
105 int CellsColor::type() const
107 return d->type;
110 QString CellsColor::name() const
112 return d->name;
115 void CellsColor::setName(const QString& name)
117 d->name = name;
120 void CellsColor::save()
122 if( !d->readOnly )
124 if( !d->fileName.isEmpty() )
126 save(d->fileName);
128 else
130 QDir brushesDir(CONFIG_DIR+"/palettes");
132 bool ok = brushesDir.exists();
133 if ( ! ok )
135 ok = brushesDir.mkdir(brushesDir.path() );
138 if( ok )
140 save(brushesDir.absoluteFilePath(d->name+".dpl"));
146 void CellsColor::save( const QString &path)
148 QFile save(path);
149 Component::PaletteDocument document(d->name, true);
151 for(int i = 0; i < columnCount() ; i++)
153 for (int j = 0; j < rowCount() ; j++)
155 QTableWidgetItem *tmpItem = itemAt(i*25, j*25);
156 if(tmpItem)
158 if(tmpItem->background().gradient())
160 document.addGradient(*tmpItem->background().gradient());
162 else if(tmpItem->background().color().isValid())
164 document.addColor(tmpItem->background().color());
169 if ( save.open(QIODevice::WriteOnly | QIODevice::Text))
171 QTextStream out(&save);
172 out << document.toString();
173 save.close();
177 void CellsColor::dragEnterEvent( QDragEnterEvent *event )
179 setFocus();
181 if (event->mimeData()->hasColor())
183 if (event->source() == this)
185 event->setDropAction(Qt::MoveAction);
186 event->accept();
188 else
190 event->acceptProposedAction();
193 else
195 event->ignore();
199 void CellsColor::dropEvent( QDropEvent *event )
201 if (event->mimeData()->hasColor())
203 QColor color = qvariant_cast<QColor>(event->mimeData()->colorData());
205 // TODO: crear item in cellscolor.cpp
207 if (event->source() == this)
209 event->setDropAction(Qt::MoveAction);
210 event->accept();
212 else
214 event->acceptProposedAction();
217 else
219 event->ignore();
223 void CellsColor::mousePressEvent(QMouseEvent* e)
225 DGui::CellView::mousePressEvent(e);
226 d->startDragPosition = e->pos();
230 void CellsColor::mouseMoveEvent(QMouseEvent* e)
232 DGui::CellView::mouseMoveEvent(e);
234 if ((e->pos() - d->startDragPosition).manhattanLength() < QApplication::startDragDistance() || !currentItem() )
235 return;
237 QDrag *drag = new QDrag( this );
238 QPixmap pix( 25, 25 );
239 QColor color= currentItem()->background().color();
240 pix.fill( color);
242 QPainter painter( &pix );
243 painter.drawRect( 0, 0, pix.width(), pix.height() );
244 painter.end();
246 QMimeData *mimeData = new QMimeData;
247 mimeData->setColorData(currentItem()->background().color());
249 drag->setMimeData(mimeData);
250 drag->setPixmap( pix );
252 /*Qt::DropAction dropAction = */drag->start(Qt::MoveAction);