📄 qdrawutil.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module 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 "qdrawutil.h"#include "qbitmap.h"#include "qpixmapcache.h"#include "qapplication.h"#include "qpainter.h"#include "qpalette.h"/*! \fn void qDrawShadeLine(QPainter *painter, int x1, int y1, int x2, int y2, const QPalette &palette, bool sunken, int lineWidth, int midLineWidth) \relates QPainter Draws a horizontal (\a y1 == \a y2) or vertical (\a x1 == \a x2) shaded line using the given \a painter. Note that nothing is drawn if \a y1 != \a y2 and \a x1 != \a x2 (i.e. the line is neither horizontal nor vertical). The provided \a palette specifies the shading colors (\l {QPalette::light()}{light}, \l {QPalette::dark()}{dark} and \l {QPalette::mid()}{middle} colors). The given \a lineWidth specifies the line width for each of the lines; it is not the total line width. The given \a midLineWidth specifies the width of a middle line drawn in the QPalette::mid() color. The line appears sunken if \a sunken is true, otherwise raised. \warning This function does not look at QWidget::style() or QApplication::style(). Use the drawing functions in QStyle to make widgets that follow the current GUI style. Alternatively you can use a QFrame widget and apply the QFrame::setFrameStyle() function to display a shaded line: \code QFrame frame: frame.setFrameStyle(QFrame::HLine | QFrame::Sunken); \endcode \sa qDrawShadeRect(), qDrawShadePanel(), QStyle*/void qDrawShadeLine(QPainter *p, int x1, int y1, int x2, int y2, const QPalette &pal, bool sunken, int lineWidth, int midLineWidth){ if (!(p && lineWidth >= 0 && midLineWidth >= 0)) { qWarning("qDrawShadeLine: Invalid parameters"); return; } int tlw = lineWidth*2 + midLineWidth; // total line width QPen oldPen = p->pen(); // save pen if (sunken) p->setPen(pal.color(QPalette::Dark)); else p->setPen(pal.light().color()); QPolygon a; int i; if (y1 == y2) { // horizontal line int y = y1 - tlw/2; if (x1 > x2) { // swap x1 and x2 int t = x1; x1 = x2; x2 = t; } x2--; for (i=0; i<lineWidth; i++) { // draw top shadow a.setPoints(3, x1+i, y+tlw-1-i, x1+i, y+i, x2-i, y+i); p->drawPolyline(a); } if (midLineWidth > 0) { p->setPen(pal.mid().color()); for (i=0; i<midLineWidth; i++) // draw lines in the middle p->drawLine(x1+lineWidth, y+lineWidth+i, x2-lineWidth, y+lineWidth+i); } if (sunken) p->setPen(pal.light().color()); else p->setPen(pal.dark().color()); for (i=0; i<lineWidth; i++) { // draw bottom shadow a.setPoints(3, x1+i, y+tlw-i-1, x2-i, y+tlw-i-1, x2-i, y+i+1); p->drawPolyline(a); } } else if (x1 == x2) { // vertical line int x = x1 - tlw/2; if (y1 > y2) { // swap y1 and y2 int t = y1; y1 = y2; y2 = t; } y2--; for (i=0; i<lineWidth; i++) { // draw left shadow a.setPoints(3, x+i, y2, x+i, y1+i, x+tlw-1, y1+i); p->drawPolyline(a); } if (midLineWidth > 0) { p->setPen(pal.mid().color()); for (i=0; i<midLineWidth; i++) // draw lines in the middle p->drawLine(x+lineWidth+i, y1+lineWidth, x+lineWidth+i, y2); } if (sunken) p->setPen(pal.light().color()); else p->setPen(pal.dark().color()); for (i=0; i<lineWidth; i++) { // draw right shadow a.setPoints(3, x+lineWidth, y2-i, x+tlw-i-1, y2-i, x+tlw-i-1, y1+lineWidth); p->drawPolyline(a); } } p->setPen(oldPen);}/*! \fn void qDrawShadeRect(QPainter *painter, int x, int y, int width, int height, const QPalette &palette, bool sunken, int lineWidth, int midLineWidth, const QBrush *fill) \relates QPainter Draws the shaded rectangle beginning at (\a x, \a y) with the given \a width and \a height using the provided \a painter. The provide \a palette specifies the shading colors (\l {QPalette::light()}{light}, \l {QPalette::dark()}{dark} and \l {QPalette::mid()}{middle} colors. The given \a lineWidth specifies the line width for each of the lines; it is not the total line width. The \a midLineWidth specifies the width of a middle line drawn in the QPalette::mid() color. The rectangle's interior is filled with the \a fill brush unless \a fill is 0. The rectangle appears sunken if \a sunken is true, otherwise raised. \warning This function does not look at QWidget::style() or QApplication::style(). Use the drawing functions in QStyle to make widgets that follow the current GUI style. Alternatively you can use a QFrame widget and apply the QFrame::setFrameStyle() function to display a shaded rectangle: \code QFrame frame: frame.setFrameStyle(QFrame::Box | QFrame::Raised); \endcode \sa qDrawShadeLine(), qDrawShadePanel(), qDrawPlainRect(), QStyle*/void qDrawShadeRect(QPainter *p, int x, int y, int w, int h, const QPalette &pal, bool sunken, int lineWidth, int midLineWidth, const QBrush *fill){ if (w == 0 || h == 0) return; if (! (w > 0 && h > 0 && lineWidth >= 0 && midLineWidth >= 0)) { qWarning("qDrawShadeRect: Invalid parameters"); return; } QPen oldPen = p->pen(); if (sunken) p->setPen(pal.dark().color()); else p->setPen(pal.light().color()); int x1=x, y1=y, x2=x+w-1, y2=y+h-1; if (lineWidth == 1 && midLineWidth == 0) {// standard shade rectangle p->drawRect(x1, y1, w-2, h-2); if (sunken) p->setPen(pal.light().color()); else p->setPen(pal.dark().color()); QLineF lines[4] = { QLineF(x1+1, y1+1, x2-2, y1+1), QLineF(x1+1, y1+2, x1+1, y2-2), QLineF(x1, y2, x2, y2), QLineF(x2,y1, x2,y2-1) }; p->drawLines(lines, 4); // draw bottom/right lines } else { // more complicated int m = lineWidth+midLineWidth; int i, j=0, k=m; for (i=0; i<lineWidth; i++) { // draw top shadow QLineF lines[4] = { QLineF(x1+i, y2-i, x1+i, y1+i), QLineF(x1+i, y1+i, x2-i, y1+i), QLineF(x1+k, y2-k, x2-k, y2-k), QLineF(x2-k, y2-k, x2-k, y1+k) }; p->drawLines(lines, 4); k++; } p->setPen(pal.mid().color()); j = lineWidth*2; for (i=0; i<midLineWidth; i++) { // draw lines in the middle p->drawRect(x1+lineWidth+i, y1+lineWidth+i, w-j-1, h-j-1); j += 2; } if (sunken) p->setPen(pal.light().color()); else p->setPen(pal.dark().color()); k = m; for (i=0; i<lineWidth; i++) { // draw bottom shadow QLineF lines[4] = { QLineF(x1+1+i, y2-i, x2-i, y2-i), QLineF(x2-i, y2-i, x2-i, y1+i+1), QLineF(x1+k, y2-k, x1+k, y1+k), QLineF(x1+k, y1+k, x2-k, y1+k) }; p->drawLines(lines, 4); k++; } } if (fill) { QBrush oldBrush = p->brush(); int tlw = lineWidth + midLineWidth; p->setPen(Qt::NoPen); p->setBrush(*fill); p->drawRect(x+tlw, y+tlw, w-2*tlw, h-2*tlw); p->setBrush(oldBrush); } p->setPen(oldPen); // restore pen}/*! \fn void qDrawShadePanel(QPainter *painter, int x, int y, int width, int height, const QPalette &palette, bool sunken, int lineWidth, const QBrush *fill) \relates QPainter Draws the shaded panel beginning at (\a x, \a y) with the given \a width and \a height using the provided \a painter and the given \a lineWidth. The given \a palette specifies the shading colors (\l {QPalette::light()}{light}, \l {QPalette::dark()}{dark} and \l {QPalette::mid()}{middle} colors). The panel's interior is filled with the \a fill brush unless \a fill is 0. The panel appears sunken if \a sunken is true, otherwise raised. \warning This function does not look at QWidget::style() or QApplication::style(). Use the drawing functions in QStyle to make widgets that follow the current GUI style. Alternatively you can use a QFrame widget and apply the QFrame::setFrameStyle() function to display a shaded panel: \code QFrame frame: frame.setFrameStyle( QFrame::Panel | QFrame::Sunken); \endcode \sa qDrawWinPanel(), qDrawShadeLine(), qDrawShadeRect(), QStyle*/void qDrawShadePanel(QPainter *p, int x, int y, int w, int h, const QPalette &pal, bool sunken, int lineWidth, const QBrush *fill){ if (w == 0 || h == 0) return; if (!(w > 0 && h > 0 && lineWidth >= 0)) { qWarning("qDrawShadePanel: Invalid parameters"); } QColor shade = pal.dark().color(); QColor light = pal.light().color(); if (fill) { if (fill->color() == shade) shade = pal.shadow().color(); if (fill->color() == light) light = pal.midlight().color(); } QPen oldPen = p->pen(); // save pen QVector<QLineF> lines; lines.reserve(2*lineWidth); if (sunken) p->setPen(shade); else p->setPen(light); int x1, y1, x2, y2; int i; x1 = x; y1 = y2 = y; x2 = x+w-2; for (i=0; i<lineWidth; i++) { // top shadow lines << QLineF(x1, y1++, x2--, y2++); } x2 = x1; y1 = y+h-2; for (i=0; i<lineWidth; i++) { // left shado lines << QLineF(x1++, y1, x2++, y2--); } p->drawLines(lines); lines.clear(); if (sunken) p->setPen(light); else p->setPen(shade); x1 = x; y1 = y2 = y+h-1; x2 = x+w-1; for (i=0; i<lineWidth; i++) { // bottom shadow lines << QLineF(x1++, y1--, x2, y2--); } x1 = x2; y1 = y; y2 = y+h-lineWidth-1; for (i=0; i<lineWidth; i++) { // right shadow lines << QLineF(x1--, y1++, x2--, y2); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -