qtgradientstopswidget.cpp
来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 884 行 · 第 1/2 页
CPP
884 行
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt Designer 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.******************************************************************************//*TRANSLATOR qdesigner_internal::QtGradientStopsWidget*/#include "qtgradientstopswidget.h"#include "qtgradientstopsmodel.h"#include <QMap>#include <QImage>#include <QPainter>#include <QScrollBar>#include <QMouseEvent>#include <QRubberBand>#include <QMenu>#include "qdebug.h"using namespace qdesigner_internal;namespace qdesigner_internal {class QtGradientStopsWidgetPrivate{ QtGradientStopsWidget *q_ptr; Q_DECLARE_PUBLIC(QtGradientStopsWidget)public: void slotStopAdded(QtGradientStop *stop); void slotStopRemoved(QtGradientStop *stop); void slotStopMoved(QtGradientStop *stop, qreal newPos); void slotStopChanged(QtGradientStop *stop, const QColor &newColor); void slotStopSelected(QtGradientStop *stop, bool selected); void slotCurrentStopChanged(QtGradientStop *stop); void slotNewStop(); void slotDelete(); void slotSelectAll(); double fromViewport(int x) const; double toViewport(double x) const; QtGradientStop *stopAt(const QPoint &viewportPos) const; QList<QtGradientStop *> stopsAt(const QPoint &viewportPos) const; void setupDrag(QtGradientStop *stop, int x); void ensureVisible(double x); // x = stop position void ensureVisible(QtGradientStop *stop); QtGradientStop *newStop(const QPoint &viewportPos); bool m_backgroundTransparent; QtGradientStopsModel *m_model; double m_handleSize; int m_scaleFactor; double m_zoom; QRubberBand *m_rubber; QPoint m_clickPos; QList<QtGradientStop *> m_stops; bool m_dragging; int m_dragOffset; QMap<QtGradientStop *, qreal> m_dragStops; QMap<qreal, QColor> m_dragOriginal;};}double QtGradientStopsWidgetPrivate::fromViewport(int x) const{ QSize size = q_ptr->viewport()->size(); int w = size.width(); int max = q_ptr->horizontalScrollBar()->maximum(); int val = q_ptr->horizontalScrollBar()->value(); return ((double)x * m_scaleFactor + w * val) / (w * (m_scaleFactor + max));}double QtGradientStopsWidgetPrivate::toViewport(double x) const{ QSize size = q_ptr->viewport()->size(); int w = size.width(); int max = q_ptr->horizontalScrollBar()->maximum(); int val = q_ptr->horizontalScrollBar()->value(); return w * (x * (m_scaleFactor + max) - val) / m_scaleFactor;}QtGradientStop *QtGradientStopsWidgetPrivate::stopAt(const QPoint &viewportPos) const{ double posY = m_handleSize / 2; QListIterator<QtGradientStop *> itStop(m_stops); while (itStop.hasNext()) { QtGradientStop *stop = itStop.next(); double posX = toViewport(stop->position()); double x = viewportPos.x() - posX; double y = viewportPos.y() - posY; if ((m_handleSize * m_handleSize / 4) > (x * x + y * y)) return stop; } return 0;}QList<QtGradientStop *> QtGradientStopsWidgetPrivate::stopsAt(const QPoint &viewportPos) const{ QList<QtGradientStop *> stops; double posY = m_handleSize / 2; QListIterator<QtGradientStop *> itStop(m_stops); while (itStop.hasNext()) { QtGradientStop *stop = itStop.next(); double posX = toViewport(stop->position()); double x = viewportPos.x() - posX; double y = viewportPos.y() - posY; if ((m_handleSize * m_handleSize / 4) > (x * x + y * y)) stops.append(stop); } return stops;}void QtGradientStopsWidgetPrivate::setupDrag(QtGradientStop *stop, int x){ m_model->setCurrentStop(stop); int viewportX = qRound(toViewport(stop->position())); m_dragOffset = x - viewportX; QList<QtGradientStop *> stops = m_stops; m_stops.clear(); QListIterator<QtGradientStop *> itStop(stops); while (itStop.hasNext()) { QtGradientStop *s = itStop.next(); if (m_model->isSelected(s) || s == stop) { m_dragStops[s] = s->position() - stop->position(); m_stops.append(s); } else { m_dragOriginal[s->position()] = s->color(); } } itStop.toFront(); while (itStop.hasNext()) { QtGradientStop *s = itStop.next(); if (!m_model->isSelected(s)) m_stops.append(s); } m_stops.removeAll(stop); m_stops.prepend(stop);}void QtGradientStopsWidgetPrivate::ensureVisible(double x){ double viewX = toViewport(x); if (viewX < 0 || viewX > q_ptr->viewport()->size().width()) { int max = q_ptr->horizontalScrollBar()->maximum(); int newVal = qRound(x * (max + m_scaleFactor) - m_scaleFactor / 2); q_ptr->horizontalScrollBar()->setValue(newVal); }}void QtGradientStopsWidgetPrivate::ensureVisible(QtGradientStop *stop){ if (!stop) return; ensureVisible(stop->position());}QtGradientStop *QtGradientStopsWidgetPrivate::newStop(const QPoint &viewportPos){ QtGradientStop *copyStop = stopAt(viewportPos); double posX = fromViewport(viewportPos.x()); QtGradientStop *stop = m_model->at(posX); if (!stop) { QColor newColor; if (copyStop) newColor = copyStop->color(); else newColor = m_model->color(posX); if (!newColor.isValid()) newColor = Qt::white; stop = m_model->addStop(posX, newColor); } return stop;}void QtGradientStopsWidgetPrivate::slotStopAdded(QtGradientStop *stop){ m_stops.append(stop); q_ptr->viewport()->update();}void QtGradientStopsWidgetPrivate::slotStopRemoved(QtGradientStop *stop){ m_stops.removeAll(stop); q_ptr->viewport()->update();}void QtGradientStopsWidgetPrivate::slotStopMoved(QtGradientStop *stop, qreal newPos){ Q_UNUSED(stop) Q_UNUSED(newPos) q_ptr->viewport()->update();}void QtGradientStopsWidgetPrivate::slotStopChanged(QtGradientStop *stop, const QColor &newColor){ Q_UNUSED(stop) Q_UNUSED(newColor) q_ptr->viewport()->update();}void QtGradientStopsWidgetPrivate::slotStopSelected(QtGradientStop *stop, bool selected){ Q_UNUSED(stop) Q_UNUSED(selected) q_ptr->viewport()->update();}void QtGradientStopsWidgetPrivate::slotCurrentStopChanged(QtGradientStop *stop){ Q_UNUSED(stop) if (!m_model) return; q_ptr->viewport()->update(); if (stop) { m_stops.removeAll(stop); m_stops.prepend(stop); }}void QtGradientStopsWidgetPrivate::slotNewStop(){ if (!m_model) return; QtGradientStop *stop = newStop(m_clickPos); if (!stop) return; m_model->clearSelection(); m_model->selectStop(stop, true); m_model->setCurrentStop(stop);}void QtGradientStopsWidgetPrivate::slotDelete(){ if (!m_model) return; m_model->deleteStops();}void QtGradientStopsWidgetPrivate::slotSelectAll(){ if (!m_model) return; m_model->selectAll();}QtGradientStopsWidget::QtGradientStopsWidget(QWidget *parent) : QAbstractScrollArea(parent){ d_ptr = new QtGradientStopsWidgetPrivate; d_ptr->q_ptr = this; d_ptr->m_backgroundTransparent = true; d_ptr->m_model = 0; d_ptr->m_handleSize = 25.0; d_ptr->m_scaleFactor = 1000; d_ptr->m_dragging = false; d_ptr->m_zoom = 1; d_ptr->m_rubber = new QRubberBand(QRubberBand::Rectangle, this); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); horizontalScrollBar()->setRange(0, (int)(d_ptr->m_scaleFactor * (d_ptr->m_zoom - 1) + 0.5)); horizontalScrollBar()->setPageStep(d_ptr->m_scaleFactor); horizontalScrollBar()->setSingleStep(4); viewport()->setAutoFillBackground(false); setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred));}QtGradientStopsWidget::~QtGradientStopsWidget(){ delete d_ptr;}QSize QtGradientStopsWidget::sizeHint() const{ return QSize(qRound(2 * d_ptr->m_handleSize), qRound(2 * d_ptr->m_handleSize) + horizontalScrollBar()->sizeHint().height());}QSize QtGradientStopsWidget::minimumSizeHint() const{ return QSize(qRound(2 * d_ptr->m_handleSize), qRound(2 * d_ptr->m_handleSize) + horizontalScrollBar()->minimumSizeHint().height());}void QtGradientStopsWidget::setBackgroundTransparent(bool transparent){ if (d_ptr->m_backgroundTransparent == transparent) return; d_ptr->m_backgroundTransparent = transparent; update();}bool QtGradientStopsWidget::backgroundTransparent() const{ return d_ptr->m_backgroundTransparent;}void QtGradientStopsWidget::setGradientStopsModel(QtGradientStopsModel *model){ if (d_ptr->m_model == model) return; if (d_ptr->m_model) { disconnect(d_ptr->m_model, SIGNAL(stopAdded(QtGradientStop *)), this, SLOT(slotStopAdded(QtGradientStop *))); disconnect(d_ptr->m_model, SIGNAL(stopRemoved(QtGradientStop *)), this, SLOT(slotStopRemoved(QtGradientStop *))); disconnect(d_ptr->m_model, SIGNAL(stopMoved(QtGradientStop *, qreal)), this, SLOT(slotStopMoved(QtGradientStop *, qreal))); disconnect(d_ptr->m_model, SIGNAL(stopChanged(QtGradientStop *, const QColor &)), this, SLOT(slotStopChanged(QtGradientStop *, const QColor &))); disconnect(d_ptr->m_model, SIGNAL(stopSelected(QtGradientStop *, bool)), this, SLOT(slotStopSelected(QtGradientStop *, bool))); disconnect(d_ptr->m_model, SIGNAL(currentStopChanged(QtGradientStop *)), this, SLOT(slotCurrentStopChanged(QtGradientStop *))); d_ptr->m_stops.clear(); } d_ptr->m_model = model; if (d_ptr->m_model) { connect(d_ptr->m_model, SIGNAL(stopAdded(QtGradientStop *)), this, SLOT(slotStopAdded(QtGradientStop *))); connect(d_ptr->m_model, SIGNAL(stopRemoved(QtGradientStop *)), this, SLOT(slotStopRemoved(QtGradientStop *))); connect(d_ptr->m_model, SIGNAL(stopMoved(QtGradientStop *, qreal)), this, SLOT(slotStopMoved(QtGradientStop *, qreal))); connect(d_ptr->m_model, SIGNAL(stopChanged(QtGradientStop *, const QColor &)), this, SLOT(slotStopChanged(QtGradientStop *, const QColor &))); connect(d_ptr->m_model, SIGNAL(stopSelected(QtGradientStop *, bool)), this, SLOT(slotStopSelected(QtGradientStop *, bool))); connect(d_ptr->m_model, SIGNAL(currentStopChanged(QtGradientStop *)), this, SLOT(slotCurrentStopChanged(QtGradientStop *))); QList<QtGradientStop *> stops = d_ptr->m_model->stops().values(); QListIterator<QtGradientStop *> itStop(stops); while (itStop.hasNext()) d_ptr->slotStopAdded(itStop.next()); QList<QtGradientStop *> selected = d_ptr->m_model->selectedStops(); QListIterator<QtGradientStop *> itSelect(selected); while (itSelect.hasNext()) d_ptr->slotStopSelected(itSelect.next(), true); d_ptr->slotCurrentStopChanged(d_ptr->m_model->currentStop()); }}void QtGradientStopsWidget::mousePressEvent(QMouseEvent *e){ if (!d_ptr->m_model) return; if (e->button() != Qt::LeftButton) return; d_ptr->m_dragging = true; d_ptr->m_dragStops.clear(); d_ptr->m_dragOriginal.clear(); d_ptr->m_clickPos = e->pos(); QtGradientStop *stop = d_ptr->stopAt(e->pos()); if (stop) { if (e->modifiers() & Qt::ControlModifier) { d_ptr->m_model->selectStop(stop, !d_ptr->m_model->isSelected(stop)); } else if (e->modifiers() & Qt::ShiftModifier) { QtGradientStop *oldCurrent = d_ptr->m_model->currentStop(); if (oldCurrent) { QMap<qreal, QtGradientStop *> stops = d_ptr->m_model->stops(); QMap<qreal, QtGradientStop *>::ConstIterator itSt = stops.constFind(oldCurrent->position()); if (itSt != stops.constEnd()) { while (itSt != stops.constFind(stop->position())) { d_ptr->m_model->selectStop(itSt.value(), true); if (oldCurrent->position() < stop->position()) itSt++; else itSt--; } } } d_ptr->m_model->selectStop(stop, true); } else { if (!d_ptr->m_model->isSelected(stop)) { d_ptr->m_model->clearSelection(); d_ptr->m_model->selectStop(stop, true); } } d_ptr->setupDrag(stop, e->pos().x()); } else { d_ptr->m_model->clearSelection(); d_ptr->m_rubber->setGeometry(QRect(d_ptr->m_clickPos, QSize())); d_ptr->m_rubber->show(); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?