#include "DoodlePch.h" namespace JusDoodle { #define DFT_VIEW_WIDTH 680 #define DFT_VIEW_HIGHT 520 #define WIN_HEIGHT_PAD 45 #define DFT_WIN_WIDTH DFT_VIEW_WIDTH #define DFT_WIN_HEIGHT (DFT_VIEW_HIGHT + WIN_HEIGHT_PAD) DoodleWindow::DoodleWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::WidgetClass) , _currentPageId(-1) , _pages() , _color(Qt::red) , _width(WIDTH_THIN) , _erase(false) , drawFlag(false) { ui->setupUi(this); ui->selectedColor->hide(); ui->btnNote->hide(); ui->btnClear->hide(); ui->btnColor->hide(); // connect(ui->statusbar, SIGNAL(messageChanged(const QString &)), // SLOT(onStatusMessageChanged(const QString &))); // connect(ui->actionBlack, SIGNAL(triggered(bool)), SLOT(colorBlackSelected(bool))); // connect(ui->actionRed, SIGNAL(triggered(bool)), SLOT(colorRedSelected(bool))); // connect(ui->actionBlue, SIGNAL(triggered(bool)), SLOT(colorBlueSelected(bool))); // connect(ui->actionThin, SIGNAL(triggered(bool)), SLOT(widthThinSelected(bool))); // connect(ui->actionMiddle, SIGNAL(triggered(bool)), SLOT(widthMiddleSelected(bool))); // connect(ui->actionThick, SIGNAL(triggered(bool)), SLOT(widthThickSelected(bool))); // connect(ui->actionErase, SIGNAL(triggered(bool)), SLOT(toolsEraseSelected(bool))); connect(ui->btnClear, SIGNAL(clicked()), SLOT(toolsClear())); // connect(ui->actionPrevious, SIGNAL(triggered()), SLOT(pagePrevious())); // connect(ui->actionNext, SIGNAL(triggered()), SLOT(pageNext())); connect(ui->btnPrev, SIGNAL(clicked()), SLOT(pagePrevious())); connect(ui->btnNext, SIGNAL(clicked()), SLOT(pageNext())); connect(ui->selectedColor,SIGNAL(itemSelectionChanged()),this,SLOT(selectedColor())); ui->btnPrev->setFocusPolicy(Qt::NoFocus); ui->btnNext->setFocusPolicy(Qt::NoFocus); } DoodleWindow::~DoodleWindow() { std::vector::iterator it; for (it = _pages.begin(); it != _pages.end(); it++) delete *it; _pages.clear(); delete ui; } void DoodleWindow::resizeEvent(QResizeEvent *) { if (_currentPageId >= 0) { QRect rect; calculateRect(ui->widget_scrawl->size(), rect); _pages[_currentPageId]->move(rect.topLeft()); _pages[_currentPageId]->resize(rect.size()); //char acBuf[64]; //sprintf(acBuf, "%dx%d %dx%d %dx%d %dx%d", // ui->centralwidget->rect().x(), ui->centralwidget->rect().y(), // ui->centralwidget->rect().width(), ui->centralwidget->rect().height(), // rect.x(), rect.y(), rect.width(), rect.height()); //ui->statusbar->showMessage(acBuf); } } void DoodleWindow::init(int pageCount) { if (_currentPageId >= 0) return; DOODLE_LOG_INFO("count %d.", pageCount); QRect rect; calculateRect(ui->widget_scrawl->size(), rect); int i; for (i = 0; i < pageCount; i++) { DoodlePage *page = new DoodlePage(i, ui->widget_scrawl); page->setGeometry(0,0,680,520); page->setColor(_color); page->setWidth(_width); page->setMode(_erase); connect(this, SIGNAL(setDrawFlag(bool)), page, SLOT(setDrawFlag(bool))); connect(this, SIGNAL(colorChanged(const QColor &)), page, SLOT(onColorChanged(const QColor &))); connect(this, SIGNAL(widthChanged(double)), page, SLOT(onWidthChanged(double))); connect(this, SIGNAL(modeChanged(bool)), page, SLOT(onModeChanged(bool))); connect(page, SIGNAL(notifyDrawPath(int, const Path &, const QColor &, double)), SIGNAL(notifyDrawPath(int, const Path &, const QColor &, double))); connect(page, SIGNAL(notifyErasePath(int, const Path &, double)), SIGNAL(notifyErasePath(int, const Path &, double))); page->move(rect.topLeft()); page->resize(rect.size()); page->hide(); page->setAttribute(Qt::WA_OpaquePaintEvent); // page->setAutoFillBackground(false); page->setAutoFillBackground(true); _pages.push_back(page); } _currentPageId = 0; _pages[_currentPageId]->show(); onStatusMessageChanged(QString()); } void DoodleWindow::setBackgroundImages(const QStringList &imagePaths) { if (_currentPageId < 0) return; unsigned int i; for (i = 0; i < _pages.size(); i++) { if (i >= (unsigned int)imagePaths.size()) break; _pages[i]->setBackground(imagePaths[i]); } qDebug()<<"DoodleWindow:"<= (int)_pages.size()) return; _pages[pageId]->setBackground(path); } void DoodleWindow::actionDraw(int pageId, unsigned int argb, double width, const Path &path) { if (pageId < 0 || pageId >= (int)_pages.size()) { DOODLE_LOG_ERROR("invalid pageid %d.", pageId); return; } QColor color((argb & 0xFF0000) >> 16, (argb & 0xFF00) >> 8, argb & 0xFF, (argb & 0xFF000000) >> 24); DOODLE_LOG_INFO("pageid %d.", pageId); _pages[pageId]->draw(path, color, width); } void DoodleWindow::actionErase(int pageId, double width, const Path &path) { if (pageId < 0 || pageId >= (int)_pages.size()) { DOODLE_LOG_ERROR("invalid pageid %d.", pageId); return; } DOODLE_LOG_INFO("pageid %d.", pageId); _pages[pageId]->erase(path, width); } void DoodleWindow::actionClean(int pageId) { if (pageId < 0 || pageId >= (int)_pages.size()) { DOODLE_LOG_ERROR("invalid pageid %d.", pageId); return; } DOODLE_LOG_INFO("pageid %d.", pageId); _pages[pageId]->clear(); } void DoodleWindow::actionSelectPage(int pageId) { if (!pageSelected(pageId)) { DOODLE_LOG_ERROR("invalid pageid %d.", pageId); return; } DOODLE_LOG_INFO("pageid %d.", pageId); } void DoodleWindow::colorBlackSelected(bool checked) { if (!checked) { // ui->actionBlack->setChecked(true); return; } // colorSelected(JusDoodle::Black); // ui->actionRed->setChecked(false); // ui->actionBlue->setChecked(false); } void DoodleWindow::colorRedSelected(bool checked) { if (!checked) { // ui->actionRed->setChecked(true); return; } colorSelected(JusDoodle::Red); // ui->actionBlack->setChecked(false); // ui->actionBlue->setChecked(false); } void DoodleWindow::colorBlueSelected(bool checked) { if (!checked) { // ui->actionBlue->setChecked(true); return; } colorSelected(JusDoodle::Blue); // ui->actionBlack->setChecked(false); // ui->actionRed->setChecked(false); } void DoodleWindow::widthThinSelected(bool checked) { if (!checked) { // ui->actionThin->setChecked(true); return; } widthSelected(JusDoodle::Thin); // ui->actionMiddle->setChecked(false); // ui->actionThick->setChecked(false); } void DoodleWindow::widthMiddleSelected(bool checked) { if (!checked) { // ui->actionMiddle->setChecked(true); return; } widthSelected(JusDoodle::Middle); // ui->actionThin->setChecked(false); // ui->actionThick->setChecked(false); } void DoodleWindow::widthThickSelected(bool checked) { if (!checked) { // ui->actionThick->setChecked(true); return; } widthSelected(JusDoodle::Thick); // ui->actionThin->setChecked(false); // ui->actionMiddle->setChecked(false); } void DoodleWindow::toolsEraseSelected(bool checked) { if (_erase != checked) { _erase = checked; emit modeChanged(checked); onStatusMessageChanged(QString()); } } void DoodleWindow::toolsClear() { if (_currentPageId >= 0) { _pages[_currentPageId]->clear(); emit notifyClean(_currentPageId); } } void DoodleWindow::pagePrevious() { if (pageSelected(_currentPageId - 1)) emit notifyPageChanged(_currentPageId); } void DoodleWindow::pageNext() { if (pageSelected(_currentPageId + 1)) emit notifyPageChanged(_currentPageId); } void DoodleWindow::onStatusMessageChanged(const QString &message) { if (message.isNull()) { QString color; if (_color == Qt::black) color = "Black"; else if (_color == Qt::red) color = "Red"; else color = "Blue"; QString width; if (_width <= WIDTH_THIN) width = "Thin"; else if (_width <= WIDTH_MIDDLE) width = "Middle"; else width = "Thick"; QString mode; if (_erase) mode = "Erase"; else mode = "Draw"; QString msg = QString("Page:%1/%2 \tColor:%3 \tWidth:%4 \tMode:%5") .arg(_currentPageId + 1) .arg(_pages.size()) .arg(color) .arg(width) .arg(mode); // ui->statusbar->showMessage(msg); } ui->labelPage->setText(QString("%1/%2").arg(_currentPageId+1).arg(_pages.size())); } void DoodleWindow::calculateRect(const QSize &size, QRect &rect) { int x, y, width, height; double ratio = size.height() * 1.0 / size.width(); if (ratio >= 16.0 / 9) { width = size.width() / 9 * 9; height = width * 16 / 9; } else { height = size.height() / 16 * 16; width = height * 9 / 16; } x = (size.width() - width) / 2; y = (size.height() - height) / 2; // change rect at one place // rect.setRect(x, y, width, height); rect.setRect(0, 0, 680, 520); // ui->widget_scrawl->setStyleSheet("background-color: rgba(255, 255, 255,255);"); } void DoodleWindow::on_btnPen_clicked() { // static QColor colorNum = _color; if(drawFlag) { drawFlag = false; ui->btnClear->hide(); ui->btnColor->hide(); } else { drawFlag = true; ui->btnClear->show(); ui->btnColor->show(); } emit setDrawFlag(drawFlag); } void DoodleWindow::selectedColor() { int colorNum = ui->selectedColor->currentRow(); if(colorNum >= 0) { colorSelected(DoodleColor(colorNum)); ui->selectedColor->hide(); } } void DoodleWindow::on_btnColor_clicked(bool checked) { if(ui->selectedColor->isVisible()) { ui->selectedColor->hide(); } else { ui->selectedColor->show(); } } void DoodleWindow::colorSelected(enum DoodleColor colorId) { QColor newColor; switch (colorId) { case JusDoodle::Red: newColor = Qt::red; break; case JusDoodle::green: newColor = Qt::green; break; case JusDoodle::Blue: newColor = Qt::blue; break; case JusDoodle::yellow: newColor = Qt::yellow; break; case JusDoodle::darkCyan: newColor = QColor(255,145,1); break; case JusDoodle::darkRed: newColor = QColor(255,64,255); break; default: return; } if (_color != newColor) { _color = newColor; emit colorChanged(newColor); } onStatusMessageChanged(QString()); } void DoodleWindow::widthSelected(enum DoodleWidth widthId) { double newWidth; switch (widthId) { case JusDoodle::Thin: newWidth = WIDTH_THIN; break; case JusDoodle::Middle: newWidth = WIDTH_MIDDLE; break; case JusDoodle::Thick: newWidth = WIDTH_THICK; break; default: return; } if (_width != newWidth) { _width = newWidth; emit widthChanged(newWidth); } onStatusMessageChanged(QString()); } bool DoodleWindow::pageSelected(int newIndex) { if (newIndex < 0 || newIndex >= (int)_pages.size()) return false; // transform show page _pages[_currentPageId]->hide(); QRect rect; calculateRect(ui->widget_scrawl->size(), rect); _pages[newIndex]->move(rect.topLeft()); _pages[newIndex]->resize(rect.size()); _pages[newIndex]->show(); _currentPageId = newIndex; onStatusMessageChanged(QString()); return true; } }