paintermodule.cpp 12 KB
Newer Older
Victor Yacovlev's avatar
Victor Yacovlev committed
1 2 3 4 5 6 7 8 9
/*
This file is generated, but you can safely change it
until you run "gen_actor_source.py" with "--project" flag.

Generated file is just a skeleton for module contents. 
You should change it corresponding to functionality.
*/

#include <QtCore>
Victor Yacovlev's avatar
Victor Yacovlev committed
10 11 12
#if QT_VERSION >= 0x050000
#include <QtWidgets>
#else
Victor Yacovlev's avatar
Victor Yacovlev committed
13
#include <QtGui>
Victor Yacovlev's avatar
Victor Yacovlev committed
14
#endif
Victor Yacovlev's avatar
Victor Yacovlev committed
15 16
#include "paintermodule.h"
#include "painterwindow.h"
17
#include "painterview.h"
Victor Yacovlev's avatar
Victor Yacovlev committed
18 19 20 21 22 23
#include "paintertools.h"

namespace ActorPainter {

PainterModule::PainterModule(ExtensionSystem::KPlugin * parent)
    : PainterModuleBase(parent)
24 25 26 27 28 29
{
    m_window = 0;
    dirty_ = true;
}

void PainterModule::createGui()
Victor Yacovlev's avatar
Victor Yacovlev committed
30 31 32
{
    m_window = new PainterWindow(this, 0);
    view = m_window->view();
33
    originalCanvas.reset(new QImage(QSize(640,480), QImage::Format_RGB32));
Victor Yacovlev's avatar
Victor Yacovlev committed
34
    originalCanvas->fill(QColor("white").rgb());
35
    canvas.reset(new QImage(QSize(640,480), QImage::Format_RGB32));
Victor Yacovlev's avatar
Victor Yacovlev committed
36 37
    canvas->fill(QColor("white").rgb());
    canvasLock = new QMutex;
38 39 40
    m_window->setCanvasSize(canvas->size());
    dirtyLock_ = new QMutex;
    startTimer(50);
Victor Yacovlev's avatar
Victor Yacovlev committed
41 42 43
    reset();
}

44 45 46 47 48 49 50 51
QString PainterModule::initialize(const QStringList &configurationParameters, const ExtensionSystem::CommandLine &)
{
    if (!configurationParameters.contains("tablesOnly")) {
        createGui();
    }
    return "";
}

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
void PainterModule::timerEvent(QTimerEvent *event)
{
    dirtyLock_->lock();
    if (dirty_) {
        canvasLock->lock();
        if (view) {
            view->setCanvasData(*canvas);
        }
        canvasLock->unlock();
        dirty_ = false;
    }
    dirtyLock_->unlock();
    event->accept();
}

void PainterModule::markViewDirty()
{
    QMutexLocker l(dirtyLock_);
    dirty_ = true;
}

73 74 75 76 77
QList<ExtensionSystem::CommandLineParameter> PainterModule::acceptableCommandLineParameters()
{
    return QList<ExtensionSystem::CommandLineParameter>();
}

Victor Yacovlev's avatar
Victor Yacovlev committed
78 79 80 81 82 83 84 85 86
void PainterModule::reset()
{
    point = QPoint(0,0);
    font = QFont();
    brush = QBrush();
    style = Qt::SolidPattern;
    transparent = false;
    pen = QPen();
    canvasLock->lock();
87 88
    canvas.reset(new QImage(originalCanvas->copy()));
    m_window->setCanvasSize(originalCanvas->size());
Victor Yacovlev's avatar
Victor Yacovlev committed
89
    canvasLock->unlock();
90
    markViewDirty();
Victor Yacovlev's avatar
Victor Yacovlev committed
91 92 93 94 95 96 97 98 99 100 101 102 103
}

void PainterModule::setAnimationEnabled(bool )
{

}


QWidget* PainterModule::mainWidget() const
{
    return m_window;
}

104 105 106 107 108 109 110 111 112 113 114 115
static Color colorFromCss(const QString & css)
{
    QColor qColor = parseColor(css);
    QColor qRgbColor = qColor.toRgb();
    Color result;
    result.r = qRgbColor.red();
    result.g = qRgbColor.green();
    result.b = qRgbColor.blue();
    result.a = qRgbColor.alpha();
    return result;
}

Victor Yacovlev's avatar
Victor Yacovlev committed
116
Color PainterModule::runCMYK(const int c, const int m, const int y, const int k)
Victor Yacovlev's avatar
Victor Yacovlev committed
117
{
Victor Yacovlev's avatar
Victor Yacovlev committed
118
    return colorFromCss(QString::fromLatin1("cmyk(%1,%2,%3,%4)").arg(c).arg(m).arg(y).arg(k));
Victor Yacovlev's avatar
Victor Yacovlev committed
119 120 121
}


Victor Yacovlev's avatar
Victor Yacovlev committed
122
Color PainterModule::runCMYKA(const int c, const int m, const int y, const int k, const int a)
Victor Yacovlev's avatar
Victor Yacovlev committed
123
{
Victor Yacovlev's avatar
Victor Yacovlev committed
124
    return colorFromCss(QString::fromLatin1("cmyka(%1,%2,%3,%4,%5)").arg(c).arg(m).arg(y).arg(k).arg(a));
Victor Yacovlev's avatar
Victor Yacovlev committed
125 126 127
}


Victor Yacovlev's avatar
Victor Yacovlev committed
128
Color PainterModule::runHSL(const int h, const int s, const int l)
Victor Yacovlev's avatar
Victor Yacovlev committed
129
{
Victor Yacovlev's avatar
Victor Yacovlev committed
130
    return colorFromCss(QString::fromLatin1("hsl(%1,%2,%3)").arg(h).arg(s).arg(l));
Victor Yacovlev's avatar
Victor Yacovlev committed
131 132 133
}


Victor Yacovlev's avatar
Victor Yacovlev committed
134
Color PainterModule::runHSLA(const int h, const int s, const int l, const int a)
Victor Yacovlev's avatar
Victor Yacovlev committed
135
{
Victor Yacovlev's avatar
Victor Yacovlev committed
136
    return colorFromCss(QString::fromLatin1("hsla(%1,%2,%3,%4)").arg(h).arg(s).arg(l).arg(a));
Victor Yacovlev's avatar
Victor Yacovlev committed
137 138 139
}


Victor Yacovlev's avatar
Victor Yacovlev committed
140
Color PainterModule::runHSV(const int h, const int s, const int v)
Victor Yacovlev's avatar
Victor Yacovlev committed
141
{
Victor Yacovlev's avatar
Victor Yacovlev committed
142
    return colorFromCss(QString::fromLatin1("hsv(%1,%2,%3)").arg(h).arg(s).arg(v));
Victor Yacovlev's avatar
Victor Yacovlev committed
143 144 145
}


Victor Yacovlev's avatar
Victor Yacovlev committed
146
Color PainterModule::runHSVA(const int h, const int s, const int v, const int a)
Victor Yacovlev's avatar
Victor Yacovlev committed
147
{
Victor Yacovlev's avatar
Victor Yacovlev committed
148
    return colorFromCss(QString::fromLatin1("hsva(%1,%2,%3,%4)").arg(h).arg(s).arg(v).arg(a));
Victor Yacovlev's avatar
Victor Yacovlev committed
149 150 151
}


Victor Yacovlev's avatar
Victor Yacovlev committed
152
Color PainterModule::runRGB(const int r, const int g, const int b)
Victor Yacovlev's avatar
Victor Yacovlev committed
153
{
Victor Yacovlev's avatar
Victor Yacovlev committed
154
    return colorFromCss(QString::fromLatin1("rgb(%1,%2,%3)").arg(r).arg(g).arg(b));
Victor Yacovlev's avatar
Victor Yacovlev committed
155 156 157
}


Victor Yacovlev's avatar
Victor Yacovlev committed
158
Color PainterModule::runRGBA(const int r, const int g, const int b, const int a)
Victor Yacovlev's avatar
Victor Yacovlev committed
159
{
Victor Yacovlev's avatar
Victor Yacovlev committed
160
    return colorFromCss(QString::fromLatin1("rgba(%1,%2,%3,%4)").arg(r).arg(g).arg(b).arg(a));
Victor Yacovlev's avatar
Victor Yacovlev committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
}


void PainterModule::runMoveTo(const int x, const int y)
{
    point.setX(x);
    point.setY(y);
}


int PainterModule::runPageHeight()
{
    return canvas->height();
}


void PainterModule::runLoadPage(const QString& fileName)
{
    if (!QFile::exists(fileName)) {
        setError(tr("File not exists: %s").arg(fileName));
    }
182
    canvas.reset(new QImage(fileName));
183
    if (m_window) {
184
        m_window->setCanvasSize(canvas->size());
185 186
        m_window->setCanvasName(QFile(fileName).fileName());
    }
187
    markViewDirty();
Victor Yacovlev's avatar
Victor Yacovlev committed
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
}


void PainterModule::runFill(const int x, const int y)
{
    if (transparent)
        return;
    QStack<QPoint> stack;
    QRgb replaceColor = canvas->pixel(x,y);
    if (replaceColor==brush.color().rgb())
        return;
    stack.push(QPoint(x,y));
    while (!stack.isEmpty()) {
        QPoint pnt = stack.pop();
        if (pnt.x()<0 || pnt.y()<0 || pnt.x()>=canvas->width() || pnt.y()>=canvas->height())
            continue;
        QRgb value = canvas->pixel(pnt);
        if (value==replaceColor) {
            canvasLock->lock();
            canvas->setPixel(pnt, brush.color().rgb());
            canvasLock->unlock();
            stack.push(QPoint(pnt.x()-1, pnt.y()));
            stack.push(QPoint(pnt.x()+1, pnt.y()));
            stack.push(QPoint(pnt.x(), pnt.y()-1));
            stack.push(QPoint(pnt.x(), pnt.y()+1));
        }
    }
215
    markViewDirty();
Victor Yacovlev's avatar
Victor Yacovlev committed
216 217 218
}


Victor Yacovlev's avatar
Victor Yacovlev committed
219
void PainterModule::runSetBrush(const Color& cs)
Victor Yacovlev's avatar
Victor Yacovlev committed
220
{
221
    QColor c(cs.r, cs.g, cs.b, cs.a);
Victor Yacovlev's avatar
Victor Yacovlev committed
222
    brush.setColor(c);
223
    brush.setStyle(style);
Victor Yacovlev's avatar
Victor Yacovlev committed
224 225 226 227 228 229
}


void PainterModule::runLine(const int x0, const int y0, const int x, const int y)
{
    canvasLock->lock();
230
    QPainter p(canvas.data());
Victor Yacovlev's avatar
Victor Yacovlev committed
231 232 233 234 235
    p.setPen(pen);
    p.setBrush(brush);
    p.drawLine(x0,y0,x,y);
    canvasLock->unlock();
    p.end();
236
    markViewDirty();
Victor Yacovlev's avatar
Victor Yacovlev committed
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
}


void PainterModule::runLineTo(const int x1, const int y1)
{
    runLine(point.x(), point.y(), x1, y1);
    point.setX(x1);
    point.setY(y1);
}

void PainterModule::runPolygon(const int N, const QVector<int> &xx, const QVector<int> &yy)
{
    QVector<QPoint> points(N);
    if (N!=xx.size() || N!=yy.size()) {
        setError(tr("Invalid points number"));
    }
    else {
        for (int i=0; i<N; i++) {
            points[i] = QPoint(xx[i], yy[i]);
        }
        drawPolygon(points);
    }
}

void PainterModule::drawPolygon(const QVector<QPoint> &points)
{
    canvasLock->lock();
264
    QPainter p(canvas.data());
Victor Yacovlev's avatar
Victor Yacovlev committed
265 266 267 268
    p.setPen(pen);
    p.setBrush(brush);
    p.drawPolygon(QPolygon(points));
    canvasLock->unlock();
269
    markViewDirty();
Victor Yacovlev's avatar
Victor Yacovlev committed
270 271 272 273 274 275
}


void PainterModule::runWrite(const int x, const int y, const QString& text)
{
    canvasLock->lock();
276
    QPainter p(canvas.data());
Victor Yacovlev's avatar
Victor Yacovlev committed
277 278 279 280 281
    p.setPen(pen);
    p.setBrush(brush);
    p.setFont(font);
    p.drawText(x,y,text);
    canvasLock->unlock();
282
    markViewDirty();
Victor Yacovlev's avatar
Victor Yacovlev committed
283 284 285
}


Victor Yacovlev's avatar
Victor Yacovlev committed
286
void PainterModule::runNewPage(const int width, const int height, const Color& backgroundColor)
Victor Yacovlev's avatar
Victor Yacovlev committed
287
{
288
    QColor clr(backgroundColor.r, backgroundColor.g, backgroundColor.b, backgroundColor.a);
289
    canvas.reset(new QImage(width,height,QImage::Format_RGB32));
Victor Yacovlev's avatar
Victor Yacovlev committed
290
    canvas->fill(clr.rgb());
291
    m_window->setCanvasSize(canvas->size());
292 293 294 295 296
    QString colorName = findColorName(backgroundColor);
    if (colorName.length() > 0 && ! colorName.contains("(") ) {
        colorName[0] = colorName[0].toUpper();
    }
    m_window->setCanvasName(QString("%1 %2x%3").arg(colorName).arg(width).arg(height));
297
    markViewDirty();
Victor Yacovlev's avatar
Victor Yacovlev committed
298 299 300 301 302
}


void PainterModule::runCircle(const int xc, const int yc, const int r)
{
Victor Yacovlev's avatar
Victor Yacovlev committed
303 304 305 306 307 308 309
    canvasLock->lock();
    QPainter p(canvas.data());
    p.setPen(pen);
    p.setBrush(brush);
    p.drawEllipse(QPoint(xc, yc), r, r);
    canvasLock->unlock();
    markViewDirty();
Victor Yacovlev's avatar
Victor Yacovlev committed
310 311 312
}


Victor Yacovlev's avatar
Victor Yacovlev committed
313
void PainterModule::runSetPen(const int width, const Color& cs)
Victor Yacovlev's avatar
Victor Yacovlev committed
314
{
315 316
    QColor c(cs.r, cs.g, cs.b, cs.a);
    if (width<1) {
Victor Yacovlev's avatar
Victor Yacovlev committed
317 318 319 320 321 322 323 324
        pen = Qt::NoPen;
    }
    else {
        pen = QPen(c, width);
    }
}


Victor Yacovlev's avatar
Victor Yacovlev committed
325
void PainterModule::runPoint(const int x, const int y, const Color& color)
Victor Yacovlev's avatar
Victor Yacovlev committed
326 327
{
    canvasLock->lock();
328
    QPainter p(canvas.data());
329 330
    const QColor qColor(color.r, color.g, color.b, color.a);
    p.setPen(QPen(qColor));
Victor Yacovlev's avatar
Victor Yacovlev committed
331 332
    p.drawPoint(x,y);
    canvasLock->unlock();
333
    markViewDirty();
Victor Yacovlev's avatar
Victor Yacovlev committed
334 335 336 337 338 339
}


void PainterModule::runDensity(const int value)
{
    int dens = value;
Victor Yacovlev's avatar
Victor Yacovlev committed
340 341
    dens = qMin(8, dens);
    dens = qMax(0, dens);
Victor Yacovlev's avatar
Victor Yacovlev committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
    if (dens==0) {
        brush.setStyle(Qt::NoBrush);
    }
    else if (dens==1) {
        brush.setStyle(Qt::Dense7Pattern);
    }
    else if (dens==2) {
        brush.setStyle(Qt::Dense6Pattern);
    }
    else if (dens==3) {
        brush.setStyle(Qt::Dense5Pattern);
    }
    else if (dens==4) {
        brush.setStyle(Qt::Dense4Pattern);
    }
    else if (dens==5) {
        brush.setStyle(Qt::Dense3Pattern);
    }
    else if (dens==6) {
        brush.setStyle(Qt::Dense2Pattern);
    }
    else if (dens==7) {
        brush.setStyle(Qt::Dense1Pattern);
    }
    else if (dens==8) {
        brush.setStyle(Qt::SolidPattern);
    }
    style = brush.style();
    if (transparent)
        brush.setStyle(Qt::NoBrush);
    else
        brush.setStyle(style);
}


void PainterModule::runRectangle(const int x0, const int y0, const int x1, const int y1)
{
    QVector<QPoint> points(4);
    points[0] = QPoint(x0,y0);
    points[1] = QPoint(x1,y0);
    points[2] = QPoint(x1,y1);
    points[3] = QPoint(x0,y1);
    drawPolygon(points);
    point.setX(x1);
    point.setY(y1);
}


Victor Yacovlev's avatar
Victor Yacovlev committed
390
void PainterModule::runSplitToCMYK(const Color& color, int& c, int& m, int& y, int& k)
Victor Yacovlev's avatar
Victor Yacovlev committed
391
{
392
    const QColor clr = QColor(color.r, color.g, color.b, color.a).toCmyk();
Victor Yacovlev's avatar
Victor Yacovlev committed
393 394 395 396 397 398 399
    c = clr.cyan();
    m = clr.magenta();
    y = clr.yellow();
    k = clr.black();
}


Victor Yacovlev's avatar
Victor Yacovlev committed
400
void PainterModule::runSplitToHSL(const Color& color, int& h, int& s, int& l)
Victor Yacovlev's avatar
Victor Yacovlev committed
401
{
402
    const QColor clr = QColor(color.r, color.g, color.b, color.a).toHsl();
Victor Yacovlev's avatar
Victor Yacovlev committed
403 404 405 406 407 408
    h = clr.hue();
    s = clr.saturation();
    l = clr.lightness();
}


Victor Yacovlev's avatar
Victor Yacovlev committed
409
void PainterModule::runSplitToHSV(const Color& color, int& h, int& s, int& v)
Victor Yacovlev's avatar
Victor Yacovlev committed
410
{
411
    const QColor clr = QColor(color.r, color.g, color.b, color.a).toHsv();
Victor Yacovlev's avatar
Victor Yacovlev committed
412 413 414 415 416 417
    h = clr.hue();
    s = clr.saturation();
    v = clr.value();
}


Victor Yacovlev's avatar
Victor Yacovlev committed
418
void PainterModule::runSplitToRGB(const Color& color, int& r, int& g, int& b)
Victor Yacovlev's avatar
Victor Yacovlev committed
419
{
420
    const QColor clr = QColor(color.r, color.g, color.b, color.a).toRgb();
Victor Yacovlev's avatar
Victor Yacovlev committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
    r = clr.red();
    g = clr.green();
    b = clr.blue();
}


void PainterModule::runSavePage(const QString& fileName)
{
    QFile f(fileName);
    if (f.open(QIODevice::WriteOnly)) {
        canvas->save(&f,"PNG");
        f.close();
    }
    else {
        setError(tr("Can't open file for writing: %s").arg(fileName));
    }
}


void PainterModule::runUnsetBrush()
{
    brush.setStyle(Qt::NoBrush);
    transparent = true;
}


Victor Yacovlev's avatar
Victor Yacovlev committed
447
Color PainterModule::runPointSample(const int x, const int y)
Victor Yacovlev's avatar
Victor Yacovlev committed
448 449 450
{
    if (x<0 || y<0) {
        setError(tr("Coordinate is less than 0"));
Victor Yacovlev's avatar
Victor Yacovlev committed
451
        return Color();
Victor Yacovlev's avatar
Victor Yacovlev committed
452 453 454
    }
    else if (x>=canvas->width() || y>=canvas->height()) {
        setError(tr("Coordinate is out of page"));
Victor Yacovlev's avatar
Victor Yacovlev committed
455
        return Color();
Victor Yacovlev's avatar
Victor Yacovlev committed
456 457 458
    }
    else {
        QColor c = QColor::fromRgb(canvas->pixel(x,y));
Victor Yacovlev's avatar
Victor Yacovlev committed
459
        Color cc;
460 461 462 463
        cc.r = c.red();
        cc.g = c.green();
        cc.b = c.blue();
        cc.a = c.alpha();
Victor Yacovlev's avatar
Victor Yacovlev committed
464
        return cc;
Victor Yacovlev's avatar
Victor Yacovlev committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
    }
}


int PainterModule::runCenterX()
{
    return canvas->width()/2;
}


int PainterModule::runCenterY()
{
    return canvas->height()/2;
}


int PainterModule::runPageWidth()
{
    return canvas->width();
}


int PainterModule::runTextWidth(const QString& text)
{
    const QFontMetrics fm(font);
    return fm.width(text);
}


void PainterModule::runSetFont(const QString& family, const int size, const bool bold, const bool italic)
{
    if (!family.isEmpty())
        font.setFamily(family);
    if (size>0)
        font.setPixelSize(size);
    font.setBold(bold);
    font.setItalic(italic);
}


Victor Yacovlev's avatar
Victor Yacovlev committed
505
void PainterModule::runEllipse(const int x_left, const int y_top, const int x_right, const int y_bottom)
Victor Yacovlev's avatar
Victor Yacovlev committed
506 507
{
    canvasLock->lock();
508
    QPainter p(canvas.data());
Victor Yacovlev's avatar
Victor Yacovlev committed
509 510
    p.setPen(pen);
    p.setBrush(brush);
Victor Yacovlev's avatar
Victor Yacovlev committed
511 512
    const QRect ellipseRect(QPoint(x_left, y_top), QPoint(x_right, y_bottom));
    p.drawEllipse(ellipseRect.center(), ellipseRect.width()/2, ellipseRect.height()/2);
Victor Yacovlev's avatar
Victor Yacovlev committed
513
    canvasLock->unlock();
514
    markViewDirty();
Victor Yacovlev's avatar
Victor Yacovlev committed
515 516
}

Victor Yacovlev's avatar
Victor Yacovlev committed
517

Victor Yacovlev's avatar
Victor Yacovlev committed
518 519
    
} // $namespace