Example #1
0
void MainWidget::paintEvent(QPaintEvent *event) {
    int xt = event->region().boundingRect().topLeft().x()/xsize;
    int yt = event->region().boundingRect().topLeft().y()/ysize;
    int xb = event->region().boundingRect().bottomRight().x()/xsize;
    int yb = event->region().boundingRect().bottomRight().y()/ysize;
    QPainter painter(this);

    if (showGrid) {
        painter.setPen(palette().dark().color());
        for (int i = xt; i <= xb; ++i)
            painter.drawLine(xsize * i, 0, xsize * i, ysize * yasc);
        for (int j = yt; j <= yb; ++j)
            painter.drawLine(0, ysize * j, xsize * xasc, ysize * j);
    }

    for (int i = xt; i <= xb; ++i) {
        for (int j = yt; j <= yb; ++j) {
            QRect rect = pixelRect(i, j);
            if (!event->region().intersect(rect).isEmpty()) {
                painter.fillRect(rect, QColor::fromRgb(background.pixel(i, j)));
                painter.setPen(QColor::fromRgb(foreground.pixel(i,j)));
                QChar c = text.at(j).at(i);
                if (c.isNull())
                    c = ' ';
                painter.drawText(rect.translated(1,0), Qt::AlignCenter, c);
            }
        }
    }
    QRect lastRect = pixelRect(lastx, lasty).adjusted(0,0,-1,-1);
    if (!event->region().intersect(lastRect).isEmpty() && hasFocus()) {
        painter.setPen(selColor);
        painter.drawRect(lastRect);
    }
    current_brush->second->onWidgetPaint(event, painter);
}
Example #2
0
void IconEditor::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    if (zoom >= 3) {
        painter.setPen(palette().foreground().color());
        for (int i = 0; i <= image.width(); ++i)
            painter.drawLine(zoom * i, 0,
                             zoom * i, zoom * image.height());
        for (int j = 0; j <= image.height(); ++j)
            painter.drawLine(0, zoom * j,
                             zoom * image.width(), zoom * j);
    }

    for (int i = 0; i < image.width(); ++i) {
        for (int j = 0; j < image.height(); ++j) {
            QRect rect = pixelRect(i, j);
            if (!event->region().intersected(rect).isEmpty()) {
                QColor color = QColor::fromRgba(image.pixel(i, j));
                if (color.alpha() < 255)
                    painter.fillRect(rect, Qt::white);
                painter.fillRect(rect, color);
            }
        }
    }
}
Example #3
0
void MainWidget::setFGImagePixel(const QPoint &pos) {
    int i = pos.x() / xsize;
    int j = pos.y() / ysize;

    if (foreground.rect().contains(i, j)) {
        foreground.setPixel(i, j, fgColor.rgb());
        update(pixelRect(i, j));
    }
}
void IconEditor::setImagePixel(const QPoint &pos, bool opaque)
{
    int i = pos.x() / zoom;
    int j = pos.y() / zoom;

    if (image.rect().contains(i, j)) {
        if (opaque) {
            image.setPixel(i, j, penColor().rgba());
        } else {
            image.setPixel(i, j, qRgba(0, 0, 0, 0));
        }
        update(pixelRect(i, j));
    }
}
Example #5
0
void MainWidget::mouseReleaseEvent(QMouseEvent *event) {
    int i = event->pos().x() / xsize;
    int j = event->pos().y() / ysize;
    if (j >= yasc || j < 0 || i >= xasc || i < 0) {
        if (current_brush->second->onMouseRelease(event, i, j, false))
            emit somethingChanged(true);
        return;
    }
    lastx = i;
    lasty = j;
    update(pixelRect(i,j));
    if (current_brush->second->onMouseRelease(event, i, j, true))
        emit somethingChanged(true);
}
Example #6
0
void MainWidget::mouseMoveEvent(QMouseEvent *event) {
    int i = event->pos().x() / xsize;
    int j = event->pos().y() / ysize;
    if (j >= yasc || j < 0 || i >= xasc || i < 0) {
        if (current_brush->second->onMouseMove(event, i, j, false))
            emit somethingChanged(true);
        return;
    }
    int oldx = lastx;
    int oldy = lasty;
    lastx = i;
    lasty = j;
    update(pixelRect(oldx,oldy));
    if (event->buttons() & Qt::LeftButton || event->buttons() & Qt::RightButton) {
        if (current_brush->second->onMouseMove(event, i, j, true))
            emit somethingChanged(true);
    } else {
        QWidget::mouseMoveEvent(event);
    }
}
Example #7
0
QRect MainWidget::pixelRect(QPoint p) const {
    return pixelRect(p.x(), p.y());
}
Example #8
0
void MainWidget::setFGImagePixel(int x, int y) {
    if (foreground.rect().contains(x, y)) {
        foreground.setPixel(x, y, fgColor.rgb());
        update(pixelRect(x, y));
    }
}
Example #9
0
void MainWidget::setBGImagePixel(int x, int y) {
    if (background.rect().contains(x, y)) {
        background.setPixel(x, y, bgColor.rgb());
        update(pixelRect(x, y));
    }
}
Example #10
0
void MainWidget::keyPressEvent(QKeyEvent *event) {
    QChar c = event->text()[0];
    if (c.isPrint()) {
        undo->push(new Text_Command(this, lastx, lasty, c));
        if (++lastx >= xasc) {
            lastx = 0;
            if (++lasty >= yasc) {
                lasty = 0;
            }
        }
        update(pixelRect(lastx, lasty));
        emit somethingChanged(true);
    } else if (event->key() == Qt::Key_Delete) {
        undo->push(new Text_Command(this, lastx, lasty, QChar()));
        emit somethingChanged(true);
    } else if (event->key() == Qt::Key_Backspace) {
        int oldx = lastx;
        int oldy = lasty;
        if (--lastx < 0) {
            lastx = xasc-1;
            if (--lasty < 0) {
                lasty = yasc-1;
            }
        }
        undo->push(new Text_Command(this, lastx, lasty, QChar()));
        update(pixelRect(oldx, oldy));
        emit somethingChanged(true);
    } else if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) {
        int oldx = lastx;
        int oldy = lasty;
        lastx = 0;
        if (++lasty >= yasc) {
            lasty = 0;
        }
        update(pixelRect(oldx, oldy).united(pixelRect(lastx, lasty)));
    } else if (event->key() == Qt::Key_Up) {
        int oldy = lasty;
        if (--lasty < 0) {
            lasty = oldy;
        }
        update(pixelRect(lastx, oldy).united(pixelRect(lastx, lasty)));
    } else if (event->key() == Qt::Key_Down) {
        int oldy = lasty;
        if (++lasty >= yasc) {
            lasty = oldy;
        }
        update(pixelRect(lastx, oldy).united(pixelRect(lastx, lasty)));
    } else if (event->key() == Qt::Key_Left) {
        int oldx = lastx;
        if (--lastx < 0) {
            lastx = oldx;
        }
        update(pixelRect(oldx, lasty).united(pixelRect(lastx, lasty)));
    } else if (event->key() == Qt::Key_Right) {
        int oldx = lastx;
        if (++lastx >= xasc) {
            lastx = oldx;
        }
        update(pixelRect(oldx, lasty).united(pixelRect(lastx, lasty)));
    } else {
        QWidget::keyPressEvent(event);
    }
}
Example #11
0
/*绘制*/
void CheckersPicture::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setViewport(p.x(),p.y(),side,side);
    painter.setWindow(0, 0, zoom*(n), zoom*(n));

    QColor dark(0xcc,0xcc,0xcc,200);

    QColor endColor(0x78,0xff,0x21,200);            /*终点颜色*/
    QColor startColor(0xea,0x76,0x0f,200);              /*起点颜色*/
    QColor capturedColor(0xed,0x50,0x62,200);       /*捕获颜色*/
    QColor normalColor(0xd6,0xb8,0x2c,200);         /*正常色*/
    QColor black(0x00, 0x00, 0x00, 200);
    QColor white(0xff, 0xff, 0xff, 220);
    QColor light(0xed,0xfc,0xdf,200);
    QColor deep(0x5a,0x61,0x54,200);

    /*绘制棋盘*/
    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
            QRect rect = pixelRect(i, j);
            if( !((i+j%2)%2) ) {
                    painter.fillRect(rect, deep);
            } else {
                painter.fillRect(rect, light);
            }
        }
    }

    int ix,jx;

    if(v.size()) {
        int type;
        for(unsigned i=0; i< v.size(); i++) {
            color==WHITE ? jx = n-1-v.at(i).y : jx = v.at(i).y;
            color==WHITE ? ix = n-1-v.at(i).x : ix = v.at(i).x;
            QRect rect = pixelRect(ix, jx);
            type = v.at(i).type;
            if(type == MOVEDFROM)
                painter.fillRect(rect, startColor);
            else if(type == MOVEDTO || type == TOKING)
                painter.fillRect(rect, endColor);
            else if(type == MOVEDTHROUGH)
                painter.fillRect(rect, normalColor);
            else if(type == DELETED)
                painter.fillRect(rect, capturedColor);
        }
    }

    int s = zoom*0.4;
    int sd = zoom*0.3;
    /*画白棋*/
    if(curstate){   /*如果棋局状态发生了改变*/
        painter.setPen(QPen(black,zoom*0.025));
        painter.setBrush(QBrush(white));
        for(int i=0; i<n; i++) {
            for(int j=0; j<n; j++) {
                /*如果是白色则从最后一行开始画*/
                color==WHITE ? jx = j+1 : jx = n-j;
                color==WHITE? ix = n-i : ix = i+1;
                if(curstate->at(i,j)==WHITE)
                    painter.drawEllipse(QPoint(zoom*(ix-0.5),zoom*(jx-0.5)),s,s);
                if(curstate->at(i,j)==WHITEKING) {
                    painter.drawEllipse(QPoint(zoom*(ix-0.5),zoom*(jx-0.5)),s,s);
                    painter.drawEllipse(QPoint(zoom*(ix-0.5),zoom*(jx-0.5)),sd,sd);
                }
            }
        }
        /*画黑棋*/
        painter.setBrush(QBrush(black));
        for(int i=0; i<n; i++) {
            for(int j=0; j<n; j++) {
                color==WHITE ? jx = j+1 : jx = n-j;
                color==WHITE ? ix = n-i : ix = i+1;
                if(curstate->at(i,j)==BLACK)
                    painter.drawEllipse(QPoint(zoom*(ix-0.5),zoom*(jx-0.5)),s,s);
                if(curstate->at(i,j)==BLACKKING) {
                    painter.drawEllipse(QPoint(zoom*(ix-0.5),zoom*(jx-0.5)),s,s);
                    painter.setPen(QPen(white,zoom*0.1));
                    painter.drawEllipse(QPoint(zoom*(ix-0.5),zoom*(jx-0.5)),sd,sd);
                    painter.setPen(QPen(black,zoom*0.1));
                }
            }
        }
    }
    if (thinking && showHourglass)
    {
        painter.setWindow(painter.viewport());
        painter.drawImage((width() - hourglass.width()) / 2, (height() - hourglass.height()) / 2, hourglass);
    }
}