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"
27 #include <QDragEnterEvent>
28 #include <QMouseEvent>
30 #include <QApplication>
33 namespace Components
{
35 struct CellsColor::Private
37 CellsColor::Type type
;
40 QPoint startDragPosition
;
43 CellsColor::CellsColor(QWidget
*parent
, Type type
)
44 : DGui::CellView(12, parent
), d(new Private
)
52 CellsColor::~CellsColor()
58 void CellsColor::setReadOnly(bool enable
)
63 bool CellsColor::isReadOnly()
68 void CellsColor::setType(Type type
)
73 int CellsColor::type()
78 QString
CellsColor::name() const
83 void CellsColor::setName(const QString
& name
)
88 void CellsColor::save( const QString
&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);
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();
119 void CellsColor::dragEnterEvent( QDragEnterEvent
*event
)
123 if (event
->mimeData()->hasColor())
125 if (event
->source() == this)
127 event
->setDropAction(Qt::MoveAction
);
132 event
->acceptProposedAction();
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
);
156 event
->acceptProposedAction();
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() )
179 QDrag
*drag
= new QDrag( this );
180 QPixmap
pix( 25, 25 );
181 QColor color
= currentItem()->background().color();
184 QPainter
painter( &pix
);
185 painter
.drawRect( 0, 0, pix
.width(), pix
.height() );
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
);