1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
/***************************************************************************
kcoloreditview.cpp - description
-------------------
begin : Sat Jul 8 09:57:28 CEST 2000
copyright : (C) 2000 by Artur Rataj
email : art@zeus.polsl.gliwice.pl
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
// include files for Qt
#include <qpainter.h>
#include <qlayout.h>
#include <qpushbutton.h>
#include <qvgroupbox.h>
#include <qlineedit.h>
#include <qcolor.h>
#include <qprinter.h>
// include files for KDE
#include <kseparator.h>
#include <klocale.h>
// application specific includes
#include "main.h"
#include "kcoloreditview.h"
#include "kcoloreditdoc.h"
#include "kcoloredit.h"
#include "palette.h"
KColorEditView::KColorEditView(QWidget *parent, const char *name) : QSplitter(parent, name) {
colorSelector = new ColorSelector(this);
colorSelector->slotSetColor(
Color( RGB_MAX_COMPONENT_VALUE, RGB_MAX_COMPONENT_VALUE, RGB_MAX_COMPONENT_VALUE, "" ));
QWidget* paletteViewArea = new QWidget(this);
QVBoxLayout* paletteLayout = new QVBoxLayout(paletteViewArea);
paletteView = new PaletteView(16, 16, 2, this, paletteViewArea);
paletteLayout->addWidget(paletteView, 10);
QHBoxLayout* layout = new QHBoxLayout();
QVBoxLayout* addColorLayout = new QVBoxLayout(4);
addColorLayout->setMargin(8);
QHBoxLayout* buttonsLayout = new QHBoxLayout(4);
QPushButton* addColorButton = new QPushButton(i18n( "Add Color" ), paletteViewArea);
connect(addColorButton, SIGNAL( clicked() ), SLOT( slotAddColor() ));
buttonsLayout->addWidget(addColorButton);
buttonsLayout->addStretch(10);
addColorLayout->addLayout(buttonsLayout);
QCheckBox* atCursorCheckBox = new QCheckBox(i18n( "At cursor" ), paletteViewArea);
connect(atCursorCheckBox, SIGNAL( toggled(bool) ), SLOT( slotAddColorAtCursor(bool) ));
addColorLayout->addWidget(atCursorCheckBox);
overwriteCheckBox = new QCheckBox(i18n( "Overwrite" ), paletteViewArea);
connect(overwriteCheckBox, SIGNAL( toggled(bool) ), SLOT( slotAddColorOverwrite(bool) ));
slotAddColorAtCursor(false);
slotAddColorOverwrite(false);
addColorLayout->addWidget(overwriteCheckBox);
addColorLayout->addStretch(10);
//KSeparator* hLine = new KSeparator(KSeparator::HLine, paletteViewArea);
//addColorLayout->addWidget(hLine);
//addColorLayout->addStretch(10);
//QCheckBox* cursorFollowsChosenColor = new QCheckBox(i18n( "Cursor follows" ), paletteViewArea);
//addColorLayout->addWidget(cursorFollowsChosenColor);
//connect(cursorFollowsChosenColor, SIGNAL( toggled(bool) ),
// paletteView, SLOT( slotCursorFollowsChosenColor(bool) ));
//cursorFollowsChosenColor->toggle();
paletteView->slotCursorFollowsChosenColor(true);
layout->addLayout(addColorLayout, 0);
QVGroupBox* colorAtCursorFrame = new QVGroupBox(i18n("Color at Cursor"), paletteViewArea);
QWidget* colorAtCursorFrameArea = new QWidget(colorAtCursorFrame);
QVBoxLayout* colorAtCursorLayout = new QVBoxLayout(colorAtCursorFrameArea, 4);
QHBoxLayout* colorNameLayout = new QHBoxLayout(0);
QLabel* nameLabel = new QLabel(i18n( "Name" ) + ": ", colorAtCursorFrameArea);
colorNameLayout->addWidget(nameLabel, 0);
colorName = new QLineEdit(colorAtCursorFrameArea);
connect(colorName, SIGNAL( textChanged(const QString&) ), SLOT( slotSetColorName(const QString&) ));
colorNameLayout->addWidget(colorName, 10);
colorAtCursorLayout->addLayout(colorNameLayout);
QGridLayout* colorAtCursorComponentsLayout = new QGridLayout(3, 6, 4);
colorAtCursorLayout->addLayout(colorAtCursorComponentsLayout);
colorAtCursorComponentsLayout->setColStretch(1, 10);
colorAtCursorComponentsLayout->addColSpacing(2, 8);
colorAtCursorComponentsLayout->setColStretch(4, 10);
colorAtCursorComponentsLayout->setColStretch(5, 10);
QLabel* hLabel = new QLabel("H: ", colorAtCursorFrameArea);
colorAtCursorComponentsLayout->addWidget(hLabel, 0, 0);
colorAtCursorHValueLabel = new QLabel("", colorAtCursorFrameArea);
setColorAtCursorComponentValueLabelSizes(colorAtCursorHValueLabel);
colorAtCursorComponentsLayout->addWidget(colorAtCursorHValueLabel, 0, 1);
QLabel* sLabel = new QLabel("S: ", colorAtCursorFrameArea);
colorAtCursorComponentsLayout->addWidget(sLabel, 1, 0);
colorAtCursorSValueLabel = new QLabel("", colorAtCursorFrameArea);
setColorAtCursorComponentValueLabelSizes(colorAtCursorSValueLabel);
colorAtCursorComponentsLayout->addWidget(colorAtCursorSValueLabel, 1, 1);
QLabel* vLabel = new QLabel("V: ", colorAtCursorFrameArea);
colorAtCursorComponentsLayout->addWidget(vLabel, 2, 0);
colorAtCursorVValueLabel = new QLabel("", colorAtCursorFrameArea);
setColorAtCursorComponentValueLabelSizes(colorAtCursorVValueLabel);
colorAtCursorComponentsLayout->addWidget(colorAtCursorVValueLabel, 2, 1);
QLabel* rLabel = new QLabel("R: ", colorAtCursorFrameArea);
colorAtCursorComponentsLayout->addWidget(rLabel, 0, 3);
colorAtCursorRValueLabel = new QLabel("", colorAtCursorFrameArea);
setColorAtCursorComponentValueLabelSizes(colorAtCursorRValueLabel);
colorAtCursorComponentsLayout->addWidget(colorAtCursorRValueLabel, 0, 4);
QLabel* gLabel = new QLabel("G: ", colorAtCursorFrameArea);
colorAtCursorComponentsLayout->addWidget(gLabel, 1, 3);
colorAtCursorGValueLabel = new QLabel("", colorAtCursorFrameArea);
setColorAtCursorComponentValueLabelSizes(colorAtCursorGValueLabel);
colorAtCursorComponentsLayout->addWidget(colorAtCursorGValueLabel, 1, 4);
QLabel* bLabel = new QLabel("B: ", colorAtCursorFrameArea);
colorAtCursorComponentsLayout->addWidget(bLabel, 2, 3);
colorAtCursorBValueLabel = new QLabel("", colorAtCursorFrameArea);
setColorAtCursorComponentValueLabelSizes(colorAtCursorBValueLabel);
colorAtCursorComponentsLayout->addWidget(colorAtCursorBValueLabel, 2, 4);
QHBoxLayout* colorAtCursorRgbStringLayout = new QHBoxLayout();
QLabel* colorAtCursorRgbStringLabel =
new QLabel("RGB " + i18n( "hex." ) + ": ", colorAtCursorFrameArea);
colorAtCursorRgbStringLayout->addWidget(colorAtCursorRgbStringLabel);
colorAtCursorRgbStringValueLabel = new QLabel("", colorAtCursorFrameArea);
colorAtCursorRgbStringValueLabel->setFixedWidth(
colorAtCursorRgbStringValueLabel->fontMetrics().width( QString("8888888") ));
colorAtCursorRgbStringLayout->addWidget(colorAtCursorRgbStringValueLabel);
colorAtCursorRgbStringLayout->addStretch();
colorAtCursorLayout->addLayout(colorAtCursorRgbStringLayout);
layout->addWidget(colorAtCursorFrame, 10);
layout->addSpacing(8);
paletteLayout->addSpacing(4);
paletteLayout->addLayout(layout);
paletteLayout->addSpacing(4);
inColorNameChanging = false;
doNotUpdateColorLabels = false;
}
KColorEditView::~KColorEditView() {
}
void KColorEditView::setColorAtCursorComponentValueLabelSizes(QLabel* const label) {
label->setMinimumWidth(label->fontMetrics().width( QString("888") ));
label->setMaximumWidth(label->fontMetrics().width( QString("88888") ));
}
KColorEditDoc *KColorEditView::document() const {
KColorEditApp *theApp=(KColorEditApp *) parentWidget();
return theApp->document();
}
void KColorEditView::print(QPrinter *pPrinter) {
QPainter printpainter;
printpainter.begin(pPrinter);
// TODO: add your printing code here
printpainter.end();
}
void KColorEditView::chooseColor(Color* const color) {
colorSelector->slotSetColor(color);
}
void KColorEditView::slotCursorPosChanged(int position) {
Palette* palette = document()->paletteHistory()->editableStream();
if(position < palette->length()) {
Color* color = palette->color(position);
QString string;
inColorNameChanging = true;
colorName->setText(color->name());
colorName->setEnabled(true);
inColorNameChanging = false;
int rComponent = color->component(Color::RED_INDEX);
int gComponent = color->component(Color::GREEN_INDEX);
int bComponent = color->component(Color::BLUE_INDEX);
colorAtCursorRValueLabel->setText(string.setNum( rComponent ));
colorAtCursorGValueLabel->setText(string.setNum( gComponent ));
colorAtCursorBValueLabel->setText(string.setNum( bComponent ));
QColor hsvColor(rComponent, gComponent, bComponent);
int hComponent;
int sComponent;
int vComponent;
hsvColor.hsv(&hComponent, &sComponent, &vComponent);
colorAtCursorHValueLabel->setText(string.setNum( hComponent ));
colorAtCursorSValueLabel->setText(string.setNum( sComponent ));
colorAtCursorVValueLabel->setText(string.setNum( vComponent ));
colorAtCursorRgbStringValueLabel->setText(string.sprintf( "%02x%02x%02x",
rComponent, gComponent, bComponent ));
} else {
colorName->setText("");
colorName->setEnabled(false);
colorAtCursorHValueLabel->setText("");
colorAtCursorSValueLabel->setText("");
colorAtCursorVValueLabel->setText("");
colorAtCursorRValueLabel->setText("");
colorAtCursorGValueLabel->setText("");
colorAtCursorBValueLabel->setText("");
colorAtCursorRgbStringValueLabel->setText("");
}
}
void KColorEditView::slotViewColorNames(bool viewColorNames) {
paletteView->slotViewColorNames(viewColorNames);
}
void KColorEditView::updateColorValueLabels() {
if(!doNotUpdateColorLabels)
slotCursorPosChanged(document()->paletteCursorPos());
}
void KColorEditView::redraw(bool newDocument) {
if(newDocument)
paletteView->setScrollBarValue(0);
paletteView->redraw();
updateColorValueLabels();
}
void KColorEditView::slotAddColor() {
Color color = colorSelector->color();
Palette* palette = document()->paletteHistory()->editableStream();
color.setName("");
int index;
if(addColorAtCursor)
index = document()->paletteCursorPos();
else
index = palette->length();
switch(addColorMode)
{
case INSERT_COLOR:
document()->insert(index, color);
break;
case REPLACE_COLOR:
if(index < palette->length())
document()->replace(index, color);
else
document()->insert(index, color);
break;
}
}
void KColorEditView::slotAddColorAtCursor(bool atCursor) {
addColorAtCursor = atCursor;
overwriteCheckBox->setEnabled(addColorAtCursor);
}
void KColorEditView::slotAddColorOverwrite(bool overwrite) {
if(overwrite)
addColorMode = REPLACE_COLOR;
else
addColorMode = INSERT_COLOR;
}
void KColorEditView::slotSetColorName(const QString& name) {
if(!inColorNameChanging) {
/*
Palette* palette = getDocument()->getPaletteHistory()->getEditableStream();
int cursorPos = getDocument()->getPaletteCursorPos();
if(cursorPos < palette->length()) {
palette->getColor(cursorPos)->setName(name);
getDocument()->setModified(true);
getDocument()->slotRedrawAllViews(this);
}
*/
Palette* palette = document()->paletteHistory()->editableStream();
int cursorPos = document()->paletteCursorPos();
if(cursorPos < palette->length()) {
Color newColor(
palette->color(cursorPos)->component(Color::RED_INDEX),
palette->color(cursorPos)->component(Color::GREEN_INDEX),
palette->color(cursorPos)->component(Color::BLUE_INDEX),
name);
doNotUpdateColorLabels = true;
document()->replace(cursorPos, newColor);
doNotUpdateColorLabels = false;
}
}
}
#include "kcoloreditview.moc"
|