00001
00002
00003
00004
00005
00006 #include <QPainter>
00007 #include <QMouseEvent>
00008 #include <QColorDialog>
00009
00010 #include "QColorSelect.h"
00011 #include "QColorSelector.h"
00012 #include "CTransferWdt.h"
00013
00014 #include "Memory/mmgr.h"
00015
00016
00017
00018
00019 #include "vstudio.h"
00020 extern CConsole * ptrConsole;
00021
00022
00023
00024
00025
00026 QColorSelect::QColorSelect(eColorSelectType eType,QWidget * parent = 0)
00027 : QGraphicsView(parent)
00028 {
00029 type = eType;
00030
00031 rSceneRect = QRectF(0,0,1,1);
00032 scene.setSceneRect(rSceneRect);
00033 this->setScene(&scene);
00034 this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00035 this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00036 this->setInteractive(true);
00037 this->setResizeAnchor(QGraphicsView::NoAnchor);
00038 this->setTransformationAnchor(QGraphicsView::NoAnchor);
00039 this->setAlignment(Qt::AlignLeft | Qt::AlignTop);
00040
00041 connect(&scene,SIGNAL(changed(const QList<QRectF> &)),this,SLOT(Changed(const QList<QRectF> &)));
00042
00043
00044 QGraphicsItem * rect = scene.addRect(QRectF(0,0,1,0.4),QPen(),QBrush(QColor(240,240,240)));
00045 rect->setAcceptedMouseButtons(false);
00046 }
00047
00048
00049 QColorSelect::~QColorSelect()
00050 {
00051
00052 }
00053
00054
00055 void QColorSelect::SetColorStop( float pos,QColor & color )
00056 {
00057 if(type == SELECT_ALPHA)
00058 {
00059 color.setRgb(color.value(),color.value(),color.value());
00060 }
00061 QColorSelector * newItem = new QColorSelector(this);
00062 newItem->translate(pos,0);
00063 newItem->setBrush(QBrush(color));
00064 newItem->setPen(QPen());
00065 newItem->setZValue(1);
00066 scene.addItem(newItem);
00067 }
00068
00069
00070 QGradientStops QColorSelect::GetColorStops()
00071 {
00072 return this->backgroundBrush().gradient()->stops();
00073 }
00074
00075
00076 void QColorSelect::ClearColorStops()
00077 {
00078 QList<QGraphicsItem *> list = scene.items();
00079
00080 for(int i = 0 ; i < list.size() ; i++)
00081 {
00082 QColorSelector * item = qgraphicsitem_cast<QColorSelector *>(list.at(i));
00083 if(item != 0)
00084 {
00085 scene.removeItem(qgraphicsitem_cast<QColorSelector *>(list.at(i)));
00086 delete item;
00087 }
00088 }
00089
00090 }
00091
00092
00093 void QColorSelect::GetGradient(QImage & img)
00094 {
00095
00096 QPainter pnt;
00097 pnt.setRenderHint(QPainter::Antialiasing,false);
00098 pnt.begin(&img);
00099 this->render(&pnt,QRectF(),transformRect,Qt::IgnoreAspectRatio);
00100 pnt.end();
00101 }
00102
00103
00104
00105
00106
00107
00108 void QColorSelect::resizeEvent( QResizeEvent *)
00109 {
00110 Transform(0,1);
00111 }
00112
00113
00114 void QColorSelect::mouseDoubleClickEvent(QMouseEvent * event)
00115 {
00116 if(event->button() == Qt::LeftButton)
00117 {
00118
00119 QGraphicsRectItem * gItem = (QGraphicsRectItem *)scene.itemAt(mapToScene(event->pos()));
00120 if(gItem)
00121 {
00122
00123 if(gItem->type() != QColorSelector::Type)
00124 {
00125
00126 QColor col = QColorDialog::getColor(); if(!col.isValid()) return;
00127
00128 if(type == SELECT_ALPHA){
00129 col.setRgb(col.value(),col.value(),col.value());
00130 }
00131
00132
00133 QColorSelector * newItem = new QColorSelector(this);
00134 newItem->translate(mapToScene(event->pos()).x(),0);
00135 newItem->setBrush(QBrush(col));
00136 newItem->setPen(QPen());
00137 newItem->setZValue(1);
00138
00139 scene.addItem(newItem);
00140 }
00141 else
00142 {
00143
00144 QColor col = QColorDialog::getColor(); if(!col.isValid()) return;
00145
00146 if(type == SELECT_ALPHA){
00147 col.setRgb(col.value(),col.value(),col.value());
00148 }
00149
00150
00151 gItem->setBrush(QBrush(col));
00152 }
00153 }
00154 }
00155 }
00156
00157
00158 void QColorSelect::mousePressEvent( QMouseEvent *event )
00159 {
00160
00161 if(event->button()== Qt::RightButton)
00162 {
00163
00164 QGraphicsRectItem * gItem = (QGraphicsRectItem *)scene.itemAt(mapToScene(event->pos()));
00165
00166 if(gItem)
00167 {
00168 if(gItem->type() == QColorSelector::Type)
00169 {
00170 scene.removeItem(gItem);
00171 delete gItem;
00172 }
00173 }
00174 }
00175 else
00176 {
00177
00178 QGraphicsView::mousePressEvent(event);
00179 }
00180 event->accept();
00181 }
00182
00183
00184
00185
00186
00187
00188 void QColorSelect::Changed(const QList<QRectF> &)
00189 {
00190 QLinearGradient gradient(0,10,1,10);
00191
00192 QList<QGraphicsItem *> list = scene.items();
00193
00194 for(int i = 0 ; i < list.size() ; i++)
00195 {
00196 if(qgraphicsitem_cast<QColorSelector *>(list.at(i)) != 0)
00197 {
00198 gradient.setColorAt(qgraphicsitem_cast<QColorSelector * >(list.at(i))->scenePos().x(),
00199 qgraphicsitem_cast<QColorSelector * >(list.at(i))->brush().color());
00200 }
00201 }
00202 this->setBackgroundBrush(QBrush(gradient));
00203
00204
00205 emit GradientChanged();
00206 }
00207
00208 void QColorSelect::Transform(float panX,float scaleX)
00209 {
00210 transformRect.setWidth(this->width() * scaleX);
00211 transformRect.setHeight(this->height());
00212 transformRect.setX(this->width()*panX/2.0);
00213 transformRect.setY(0);
00214
00215 this->resetMatrix();
00216 this->translate(transformRect.x(),0);
00217 this->scale(transformRect.width(),transformRect.height());
00218
00219 QList<QGraphicsItem *> list = scene.items();
00220
00221 for(int i = 0 ; i < list.size() ; i++)
00222 {
00223 if(qgraphicsitem_cast<QColorSelector *>(list.at(i)) != 0)
00224 {
00225 (qgraphicsitem_cast<QColorSelector * >(list.at(i)))->ScaleX(1.0/scaleX);
00226 }
00227 }
00228 }
00229