File indexing completed on 2024-05-19 05:42:23

0001 // ct_lvtqtw_tool_rubberband.cpp                                                                               -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #include <ct_lvtqtc_graphicsview.h>
0021 #include <ct_lvtqtc_tool_rubberband.h>
0022 #include <ct_lvtshr_functional.h>
0023 
0024 #include <QBrush>
0025 #include <QDebug>
0026 #include <QMouseEvent>
0027 #include <QPen>
0028 
0029 namespace Codethink::lvtqtc {
0030 
0031 struct ToolRubberband::Private {
0032     bool rubberBandActive = false;
0033     QPoint rubberBandStart;
0034     QPoint rubberBandEnd;
0035 
0036     QBrush brush;
0037     QPen pen;
0038 };
0039 
0040 ToolRubberband::ToolRubberband(const QString& name, const QString& tooltip, const QIcon& icon, GraphicsView *gv):
0041     ITool(name, tooltip, icon, gv), d(std::make_unique<ToolRubberband::Private>())
0042 {
0043 }
0044 
0045 ToolRubberband::~ToolRubberband() noexcept = default;
0046 
0047 void ToolRubberband::mousePressEvent(QMouseEvent *event)
0048 {
0049     d->rubberBandActive = true;
0050     d->rubberBandStart = event->pos();
0051 }
0052 
0053 void ToolRubberband::mouseMoveEvent(QMouseEvent *event)
0054 {
0055     if (!d->rubberBandActive) {
0056         return;
0057     }
0058 
0059     d->rubberBandEnd = event->pos();
0060     const int left = std::min(d->rubberBandStart.x(), d->rubberBandEnd.x());
0061     const int right = std::max(d->rubberBandStart.x(), d->rubberBandEnd.x());
0062     const int top = std::min(d->rubberBandStart.y(), d->rubberBandEnd.y());
0063     const int bottom = std::max(d->rubberBandStart.y(), d->rubberBandEnd.y());
0064     rubberbandChanged(QPoint(left, top), QPoint(right, bottom));
0065     graphicsView()->viewport()->update();
0066 }
0067 
0068 void ToolRubberband::mouseReleaseEvent(QMouseEvent *event)
0069 {
0070     using Codethink::lvtshr::ScopeExit;
0071     ScopeExit _([&]() {
0072         deactivate();
0073     });
0074 
0075     if (!d->rubberBandActive) {
0076         return;
0077     }
0078 
0079     const int left = std::min(d->rubberBandStart.x(), d->rubberBandEnd.x());
0080     const int right = std::max(d->rubberBandStart.x(), d->rubberBandEnd.x());
0081     const int top = std::min(d->rubberBandStart.y(), d->rubberBandEnd.y());
0082     const int bottom = std::max(d->rubberBandStart.y(), d->rubberBandEnd.y());
0083     rubberbandFinished(QPoint(left, top), QPoint(right, bottom));
0084     event->accept();
0085 }
0086 
0087 void ToolRubberband::deactivate()
0088 {
0089     d->rubberBandActive = false;
0090     graphicsView()->viewport()->update();
0091     ITool::deactivate();
0092 }
0093 
0094 void ToolRubberband::drawForeground(QPainter *painter, const QRectF& rect)
0095 {
0096     Q_UNUSED(rect);
0097     if (!d->rubberBandActive) {
0098         return;
0099     }
0100 
0101     painter->save();
0102     painter->setWorldMatrixEnabled(false);
0103     painter->setPen(d->pen);
0104     painter->setBrush(d->brush);
0105     painter->drawRect(QRect(d->rubberBandStart, d->rubberBandEnd));
0106     painter->setWorldMatrixEnabled(true);
0107     painter->restore();
0108 }
0109 
0110 void ToolRubberband::setRubberbandBrush(const QBrush& brush)
0111 {
0112     d->brush = brush;
0113 }
0114 
0115 void ToolRubberband::setRubberbandPen(const QPen& pen)
0116 {
0117     d->pen = pen;
0118 }
0119 
0120 } // namespace Codethink::lvtqtc