1 /***************************************************************************
2 * Copyright (C) 2005 by Jorge Cuadrado *
3 * kuadrosxx@gmail.com *
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. *
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. *
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"
28 #include <QDragEnterEvent>
29 #include <QMouseEvent>
31 #include <QApplication>
34 #include "paletteparser.h"
38 struct CellsColor::Private
40 CellsColor::Type type
;
43 QPoint startDragPosition
;
48 CellsColor::CellsColor(QWidget
*parent
)
49 : DGui::CellView(12, parent
), d(new Private
)
57 CellsColor::~CellsColor()
64 bool CellsColor::load(const QString
&file
)
71 QList
<QBrush
> brushes
= parser
.brushes();
73 foreach(QBrush brush
, brushes
)
78 d
->name
= parser
.paletteName();
79 d
->readOnly
= !parser
.isEditable();
86 dError() << "Error while parse palette file: " << file
;
90 void CellsColor::setReadOnly(bool enable
)
95 bool CellsColor::isReadOnly() const
100 void CellsColor::setType(Type type
)
105 int CellsColor::type() const
110 QString
CellsColor::name() const
115 void CellsColor::setName(const QString
& name
)
120 void CellsColor::save()
124 if( !d
->fileName
.isEmpty() )
130 QDir
brushesDir(CONFIG_DIR
+"/palettes");
132 bool ok
= brushesDir
.exists();
135 ok
= brushesDir
.mkdir(brushesDir
.path() );
140 save(brushesDir
.absoluteFilePath(d
->name
+".dpl"));
146 void CellsColor::save( const QString
&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);
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();
177 void CellsColor::dragEnterEvent( QDragEnterEvent
*event
)
181 if (event
->mimeData()->hasColor())
183 if (event
->source() == this)
185 event
->setDropAction(Qt::MoveAction
);
190 event
->acceptProposedAction();
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
);
214 event
->acceptProposedAction();
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() )
237 QDrag
*drag
= new QDrag( this );
238 QPixmap
pix( 25, 25 );
239 QColor color
= currentItem()->background().color();
242 QPainter
painter( &pix
);
243 painter
.drawRect( 0, 0, pix
.width(), pix
.height() );
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
);