qpixeltool.cpp
来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 525 行 · 第 1/2 页
CPP
525 行
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the tools applications of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qpixeltool.h"#include <QtAssistant/QAssistantClient>#include <qapplication.h>#include <qdesktopwidget.h>#include <qapplication.h>#include <qclipboard.h>#include <qpainter.h>#include <qevent.h>#include <qfiledialog.h>#include <qsettings.h>#include <qmenu.h>#include <qactiongroup.h>#include <QtCore/QLibraryInfo>#include <qdebug.h>QPixelTool::QPixelTool(QWidget *parent) : QWidget(parent), m_assistantClient(0){ QSettings settings(QLatin1String("Trolltech"), QLatin1String("QPixelTool")); m_freeze = false; m_autoUpdate = settings.value(QLatin1String("autoUpdate"), 0).toBool(); m_gridSize = settings.value(QLatin1String("gridSize"), 1).toInt(); m_gridActive = settings.value(QLatin1String("gridActive"), 1).toInt(); m_displayGridSize = false; m_displayGridSizeId = 0; m_zoom = settings.value(QLatin1String("zoom"), 4).toInt(); m_displayZoom = false; m_displayZoomId = 0; m_currentColor = 0; m_mouseDown = false; m_initialSize = settings.value(QLatin1String("initialSize"), QSize(250, 200)).toSize(); move(settings.value(QLatin1String("position")).toPoint()); setMouseTracking(true); setAttribute(Qt::WA_NoBackground); m_updateId = startTimer(30);}QPixelTool::~QPixelTool(){ QSettings settings(QLatin1String("Trolltech"), QLatin1String("QPixelTool")); settings.setValue(QLatin1String("autoUpdate"), int(m_autoUpdate)); settings.setValue(QLatin1String("gridSize"), m_gridSize); settings.setValue(QLatin1String("gridActive"), m_gridActive); settings.setValue(QLatin1String("zoom"), m_zoom); settings.setValue(QLatin1String("initialSize"), size()); settings.setValue(QLatin1String("position"), pos());}void QPixelTool::timerEvent(QTimerEvent *event){ if (event->timerId() == m_updateId && !m_freeze) { grabScreen(); } else if (event->timerId() == m_displayZoomId) { killTimer(m_displayZoomId); setZoomVisible(false); } else if (event->timerId() == m_displayGridSizeId) { killTimer(m_displayGridSizeId); m_displayGridSize = false; }}void render_string(QPainter *p, int w, int h, const QString &text, int flags){ p->setBrush(QColor(255, 255, 255, 191)); p->setPen(Qt::black); QRect bounds; p->drawText(0, 0, w, h, Qt::TextDontPrint | flags, text, &bounds); if (bounds.x() == 0) bounds.adjust(0, 0, 10, 0); else bounds.adjust(-10, 0, 0, 0); if (bounds.y() == 0) bounds.adjust(0, 0, 0, 10); else bounds.adjust(0, -10, 0, 0); p->drawRect(bounds); p->drawText(bounds, flags, text);}void QPixelTool::paintEvent(QPaintEvent *){ QPainter p(this); int w = width(); int h = height(); if (m_zoom <= 4) { int wext = width() % m_zoom; int hext = height() % m_zoom; p.drawPixmap(0, 0, width() + wext, height() + hext, m_buffer); } else { p.setPen(Qt::NoPen); QImage im = m_buffer.toImage().convertToFormat(QImage::Format_RGB32); for (int y=0; y<h; y+=m_zoom) { int y_px = qMin(im.height()-1, y/m_zoom); for (int x=0; x<w; x+=m_zoom) { int x_px = qMin(im.width()-1, x/m_zoom); p.setBrush(QColor(im.pixel(x_px, y_px))); p.drawRect(x, y, m_zoom, m_zoom); } } } // Draw the grid on top. if (m_gridActive) { p.setPen(m_gridActive == 1 ? Qt::black : Qt::white); int incr = m_gridSize * m_zoom; for (int x=0; x<w; x+=incr) p.drawLine(x, 0, x, h); for (int y=0; y<h; y+=incr) p.drawLine(0, y, w, y); } QFont f(QLatin1String("courier")); f.setBold(true); p.setFont(f); if (m_displayZoom) { render_string(&p, w, h, QString(QLatin1String("Zoom: x%1")).arg(m_zoom), Qt::AlignBottom | Qt::AlignRight); } if (m_displayGridSize) { render_string(&p, w, h, QString(QLatin1String("Grid size: %1")).arg(m_gridSize), Qt::AlignBottom | Qt::AlignLeft); } if (m_freeze) { QString str; str.sprintf("Pixel: %6X\nRed: %3d\nGreen: %3d\nBlue: %3d", 0x00ffffff & m_currentColor, (0x00ff0000 & m_currentColor) >> 16, (0x0000ff00 & m_currentColor) >> 8, (0x000000ff & m_currentColor)); render_string(&p, w, h, str, Qt::AlignTop | Qt::AlignLeft); } if (m_mouseDown && m_dragStart != m_dragCurrent) { int x1 = (m_dragStart.x() / m_zoom) * m_zoom; int y1 = (m_dragStart.y() / m_zoom) * m_zoom; int x2 = (m_dragCurrent.x() / m_zoom) * m_zoom; int y2 = (m_dragCurrent.y() / m_zoom) * m_zoom; QRect r = QRect(x1, y1, x2 - x1, y2 - y1).normalized(); p.setBrush(Qt::NoBrush); p.setPen(QPen(Qt::red, 3, Qt::SolidLine)); p.drawRect(r); p.setPen(QPen(Qt::black, 1, Qt::SolidLine)); p.drawRect(r); QString str; str.sprintf("Rect: x=%d, y=%d, w=%d, h=%d", r.x() / m_zoom, r.y() / m_zoom, r.width() / m_zoom, r.height() / m_zoom); render_string(&p, w, h, str, Qt::AlignBottom | Qt::AlignLeft); }}void QPixelTool::keyPressEvent(QKeyEvent *e){ switch (e->key()) { case Qt::Key_Space: toggleFreeze(); break; case Qt::Key_Plus: setZoom(m_zoom + 1); break; case Qt::Key_Minus: setZoom(m_zoom - 1); break; case Qt::Key_PageUp: setGridSize(m_gridSize + 1); break; case Qt::Key_PageDown: setGridSize(m_gridSize - 1); break; case Qt::Key_G: toggleGrid(); break; case Qt::Key_A: m_autoUpdate = !m_autoUpdate; break; case Qt::Key_C: if (e->modifiers() & Qt::ControlModifier) copyToClipboard(); break; case Qt::Key_S: if (e->modifiers() & Qt::ControlModifier) { releaseKeyboard(); saveToFile(); } break; case Qt::Key_Control: grabKeyboard(); break; case Qt::Key_F1: showHelp(); break; }}void QPixelTool::keyReleaseEvent(QKeyEvent *e){ switch(e->key()) { case Qt::Key_Control: releaseKeyboard(); break; default:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?