1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
|
// Copyright(c) 2017-2018 Alejandro Sirgo Rica & Contributors
//
// This file is part of Flameshot.
//
// Flameshot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Flameshot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Flameshot. If not, see <http://www.gnu.org/licenses/>.
// Based on Lightscreen areadialog.cpp, Copyright 2017 Christian Kaiser <info@ckaiser.com.ar>
// released under the GNU GPL2 <https://www.gnu.org/licenses/gpl-2.0.txt>
// Based on KDE's KSnapshot regiongrabber.cpp, revision 796531, Copyright 2007 Luca Gugelmann <lucag@student.ethz.ch>
// released under the GNU LGPL <http://www.gnu.org/licenses/old-licenses/library.txt>
#include "capturewidget.h"
#include "src/widgets/capture/hovereventfilter.h"
#include "src/widgets/panel/sidepanelwidget.h"
#include "src/utils/colorutils.h"
#include "src/utils/globalvalues.h"
#include "src/widgets/capture/notifierbox.h"
#include "src/widgets/capture/colorpicker.h"
#include "src/utils/screengrabber.h"
#include "src/utils/systemnotification.h"
#include "src/utils/screenshotsaver.h"
#include "src/core/controller.h"
#include "src/widgets/capture/modificationcommand.h"
#include <QUndoView>
#include <QScreen>
#include <QGuiApplication>
#include <QApplication>
#include <QShortcut>
#include <QPainter>
#include <QPaintEvent>
#include <QMouseEvent>
#include <QBuffer>
#include <QDesktopWidget>
// CaptureWidget is the main component used to capture the screen. It contains an
// are of selection with its respective buttons.
// enableSaveWIndow
CaptureWidget::CaptureWidget(const uint id, const QString &savePath,
bool fullScreen, QWidget *parent) :
QWidget(parent), m_mouseIsClicked(false), m_rightClick(false),
m_newSelection(false), m_grabbing(false), m_captureDone(false),
m_previewEnabled(true), m_activeButton(nullptr),
m_activeTool(nullptr), m_toolWidget(nullptr),
m_mouseOverHandle(SelectionWidget::NO_SIDE), m_id(id)
{
// Base config of the widget
m_eventFilter = new HoverEventFilter(this);
connect(m_eventFilter, &HoverEventFilter::hoverIn,
this, &CaptureWidget::childEnter);
connect(m_eventFilter, &HoverEventFilter::hoverOut,
this, &CaptureWidget::childLeave);
setAttribute(Qt::WA_DeleteOnClose);
m_showInitialMsg = m_config.showHelpValue();
m_opacity = m_config.contrastOpacityValue();
setMouseTracking(true);
initContext(savePath, fullScreen);
initShortcuts();
#ifdef Q_OS_WIN
// Top left of the whole set of screens
QPoint topLeft(0,0);
#endif
if (fullScreen) {
// Grab Screenshot
bool ok = true;
m_context.screenshot = ScreenGrabber().grabEntireDesktop(ok);
if(!ok) {
SystemNotification().sendMessage(tr("Unable to capture screen"));
this->close();
}
m_context.origScreenshot = m_context.screenshot;
#ifdef Q_OS_WIN
setWindowFlags(Qt::WindowStaysOnTopHint
| Qt::FramelessWindowHint
| Qt::Popup);
for (QScreen *const screen : QGuiApplication::screens()) {
QPoint topLeftScreen = screen->geometry().topLeft();
if (topLeft.x() > topLeftScreen.x() ||
topLeft.y() > topLeftScreen.y()) {
topLeft = topLeftScreen;
}
}
move(topLeft);
#else
setWindowFlags(Qt::BypassWindowManagerHint
| Qt::WindowStaysOnTopHint
| Qt::FramelessWindowHint
| Qt::Tool);
#endif
resize(pixmap().size());
}
// Create buttons
m_buttonHandler = new ButtonHandler(this);
updateButtons();
QVector<QRect> areas;
if (m_context.fullscreen) {
for (QScreen *const screen : QGuiApplication::screens()) {
QRect r = screen->geometry();
#ifdef Q_OS_WIN
r.moveTo(r.topLeft() - topLeft);
#endif
areas.append(r);
}
} else {
areas.append(rect());
}
m_buttonHandler->updateScreenRegions(areas);
m_buttonHandler->hide();
initSelection();
updateCursor();
// Init color picker
m_colorPicker = new ColorPicker(this);
connect(m_colorPicker, &ColorPicker::colorSelected,
this, &CaptureWidget::setDrawColor);
m_colorPicker->hide();
// Init notification widget
m_notifierBox = new NotifierBox(this);
m_notifierBox->hide();
connect(&m_undoStack, &QUndoStack::indexChanged,
this, [this](int){ this->update(); });
initPanel();
}
CaptureWidget::~CaptureWidget() {
if (m_captureDone) {
emit captureTaken(m_id, this->pixmap());
} else {
emit captureFailed(m_id);
}
m_config.setdrawThickness(m_context.thickness);
}
// redefineButtons retrieves the buttons configured to be shown with the
// selection in the capture
void CaptureWidget::updateButtons() {
m_uiColor = m_config.uiMainColorValue();
m_contrastUiColor = m_config.uiContrastColorValue();
auto buttons = m_config.getButtons();
QVector<CaptureButton*> vectorButtons;
for (const CaptureButton::ButtonType &t: buttons) {
CaptureButton *b = new CaptureButton(t, this);
if (t == CaptureButton::TYPE_SELECTIONINDICATOR) {
m_sizeIndButton = b;
}
b->setColor(m_uiColor);
makeChild(b);
connect(b, &CaptureButton::pressedButton, this, &CaptureWidget::setState);
connect(b->tool(), &CaptureTool::requestAction,
this, &CaptureWidget::handleButtonSignal);
vectorButtons << b;
}
m_buttonHandler->setButtons(vectorButtons);
}
QPixmap CaptureWidget::pixmap() {
QPixmap p;
if (m_toolWidget && m_activeTool) {
p = m_context.selectedScreenshotArea().copy();
QPainter painter(&p);
m_activeTool->process(painter, p);
} else {
p = m_context.selectedScreenshotArea();
}
return m_context.selectedScreenshotArea();
}
void CaptureWidget::deleteToolwidgetOrClose() {
if (m_toolWidget) {
m_toolWidget->deleteLater();
m_toolWidget = nullptr;
} else {
close();
}
}
void CaptureWidget::paintEvent(QPaintEvent *) {
QPainter painter(this);
painter.drawPixmap(0, 0, m_context.screenshot);
if (m_activeTool && m_mouseIsClicked) {
painter.save();
m_activeTool->process(painter, m_context.screenshot);
painter.restore();
} else if (m_activeButton && m_activeButton->tool()->showMousePreview() &&
m_previewEnabled)
{
painter.save();
m_activeButton->tool()->paintMousePreview(painter, m_context);
painter.restore();
}
QColor overlayColor(0, 0, 0, m_opacity);
painter.setBrush(overlayColor);
QRect r;
if (m_selection->isVisible()) {
r = m_selection->geometry().normalized().adjusted(0, 0, -1, -1);
}
QRegion grey(rect());
grey = grey.subtracted(r);
painter.setClipRegion(grey);
painter.drawRect(-1, -1, rect().width() + 1, rect().height() + 1);
painter.setClipRect(rect());
if (m_showInitialMsg) {
QRect helpRect = QGuiApplication::primaryScreen()->geometry();
helpRect.moveTo(mapFromGlobal(helpRect.topLeft()));
QString helpTxt = tr("Select an area with the mouse, or press Esc to exit."
"\nPress Enter to capture the screen."
"\nPress Right Click to show the color picker."
"\nUse the Mouse Wheel to change the thickness of your tool."
"\nPress Space to open the side panel.");
// We draw the white contrasting background for the text, using the
//same text and options to get the boundingRect that the text will have.
QRectF bRect = painter.boundingRect(helpRect, Qt::AlignCenter, helpTxt);
// These four calls provide padding for the rect
const int margin = QApplication::fontMetrics().height() / 2;
bRect.setWidth(bRect.width() + margin);
bRect.setHeight(bRect.height() + margin);
bRect.setX(bRect.x() - margin);
bRect.setY(bRect.y() - margin);
QColor rectColor(m_uiColor);
rectColor.setAlpha(180);
QColor textColor((ColorUtils::colorIsDark(rectColor) ?
Qt::white : Qt::black));
painter.setBrush(QBrush(rectColor, Qt::SolidPattern));
painter.setPen(QPen(textColor));
painter.drawRect(bRect);
painter.drawText(helpRect, Qt::AlignCenter, helpTxt);
}
if (m_selection->isVisible()) {
// paint handlers
painter.setPen(m_uiColor);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(m_uiColor);
for(auto r: m_selection->handlerAreas()) {
painter.drawRoundRect(r, 100, 100);
}
}
}
void CaptureWidget::mousePressEvent(QMouseEvent *e) {
if (e->button() == Qt::RightButton) {
m_rightClick = true;
m_colorPicker->move(e->pos().x()-m_colorPicker->width()/2,
e->pos().y()-m_colorPicker->height()/2);
m_colorPicker->show();
} else if (e->button() == Qt::LeftButton) {
m_showInitialMsg = false;
m_mouseIsClicked = true;
// Click using a tool
if (m_activeButton) {
if (m_activeTool) {
if (m_activeTool->isValid() && m_toolWidget) {
pushToolToStack();
} else {
m_activeTool->deleteLater();
}
if (m_toolWidget) {
m_toolWidget->deleteLater();
return;
}
}
m_activeTool = m_activeButton->tool()->copy(this);
connect(this, &CaptureWidget::colorChanged,
m_activeTool, &CaptureTool::colorChanged);
connect(this, &CaptureWidget::thicknessChanged,
m_activeTool, &CaptureTool::thicknessChanged);
connect(m_activeTool, &CaptureTool::requestAction,
this, &CaptureWidget::handleButtonSignal);
m_activeTool->drawStart(m_context);
return;
}
m_dragStartPoint = e->pos();
m_selection->saveGeometry();
// New selection
if (!m_selection->geometry().contains(e->pos()) &&
m_mouseOverHandle == SelectionWidget::NO_SIDE)
{
m_selection->setGeometry(QRect(e->pos(), e->pos()));
m_selection->setVisible(false);
m_newSelection = true;
m_buttonHandler->hide();
update();
} else {
m_grabbing = true;
}
}
updateCursor();
}
void CaptureWidget::mouseMoveEvent(QMouseEvent *e) {
m_context.mousePos = e->pos();
if (m_mouseIsClicked && !m_activeButton) {
if (m_buttonHandler->isVisible()) {
m_buttonHandler->hide();
}
if (m_newSelection) {
m_selection->setVisible(true);
m_selection->setGeometry(
QRect(m_dragStartPoint, m_context.mousePos).normalized());
update();
} else if (m_mouseOverHandle == SelectionWidget::NO_SIDE) {
// Moving the whole selection
QRect initialRect = m_selection->savedGeometry().normalized();
QPoint newTopLeft = initialRect.topLeft() + (e->pos() - m_dragStartPoint);
QRect finalRect(newTopLeft, initialRect.size());
if (finalRect.left() < rect().left()) {
finalRect.setLeft(rect().left());
} else if (finalRect.right() > rect().right()) {
finalRect.setRight(rect().right());
}
if (finalRect.top() < rect().top()) {
finalRect.setTop(rect().top());
} else if (finalRect.bottom() > rect().bottom()) {
finalRect.setBottom(rect().bottom());
}
m_selection->setGeometry(finalRect.normalized().intersected(rect()));
update();
} else {
// Dragging a handle
QRect r = m_selection->savedGeometry();
QPoint offset = e->pos() - m_dragStartPoint;
bool symmetryMod = qApp->keyboardModifiers() & Qt::ShiftModifier;
using sw = SelectionWidget;
if (m_mouseOverHandle == sw::TOPLEFT_SIDE
|| m_mouseOverHandle == sw::TOP_SIDE
|| m_mouseOverHandle == sw::TOPRIGHT_SIDE)
{ // dragging one of the top handles
r.setTop(r.top() + offset.y());
if (symmetryMod) {
r.setBottom(r.bottom() - offset.y());
}
}
if (m_mouseOverHandle == sw::TOPLEFT_SIDE
|| m_mouseOverHandle == sw::LEFT_SIDE
|| m_mouseOverHandle == sw::BOTTONLEFT_SIDE)
{ // dragging one of the left handles
r.setLeft(r.left() + offset.x());
if (symmetryMod) {
r.setRight(r.right() - offset.x());
}
}
if (m_mouseOverHandle == sw::BOTTONLEFT_SIDE
|| m_mouseOverHandle == sw::BOTTON_SIDE
|| m_mouseOverHandle == sw::BOTTONRIGHT_SIDE)
{ // dragging one of the bottom handles
r.setBottom(r.bottom() + offset.y());
if (symmetryMod) {
r.setTop(r.top() - offset.y());
}
}
if (m_mouseOverHandle == sw::TOPRIGHT_SIDE
|| m_mouseOverHandle == sw::RIGHT_SIDE
|| m_mouseOverHandle == sw::BOTTONRIGHT_SIDE)
{ // dragging one of the right handles
r.setRight(r.right() + offset.x());
if (symmetryMod) {
r.setLeft(r.left() - offset.x());
}
}
m_selection->setGeometry(r.intersected(rect()).normalized());
update();
}
} else if (m_mouseIsClicked && m_activeTool) {
// drawing with a tool
m_activeTool->drawMove(e->pos());
update();
// Hides the buttons under the mouse. If the mouse leaves, it shows them.
if (m_buttonHandler->buttonsAreInside()) {
const bool containsMouse = m_buttonHandler->contains(m_context.mousePos);
if (containsMouse) {
m_buttonHandler->hide();
} else {
m_buttonHandler->show();
}
}
} else if (m_activeButton && m_activeButton->tool()->showMousePreview()) {
update();
} else {
if (!m_selection->isVisible()) {
return;
}
m_mouseOverHandle = m_selection->getMouseSide(m_context.mousePos);
updateCursor();
}
}
void CaptureWidget::mouseReleaseEvent(QMouseEvent *e) {
if (e->button() == Qt::RightButton) {
m_colorPicker->hide();
m_rightClick = false;
// when we end the drawing we have to register the last point and
//add the temp modification to the list of modifications
} else if (m_mouseIsClicked && m_activeTool) {
m_activeTool->drawEnd(m_context.mousePos);
if (m_activeTool->isValid()) {
pushToolToStack();
} else if (!m_toolWidget){
m_activeTool->deleteLater();
m_activeTool = nullptr;
}
}
// Show the buttons after the resize of the selection or the creation
// of a new one.
if (!m_buttonHandler->isVisible() && m_selection->isVisible()) {
// Don't go outside
QRect newGeometry = m_selection->geometry().intersected(rect());
// normalize
if (newGeometry.width() <= 0) {
int left = newGeometry.left();
newGeometry.setLeft(newGeometry.right());
newGeometry.setRight(left);
}
if (newGeometry.height() <= 0) {
int top = newGeometry.top();
newGeometry.setTop(newGeometry.bottom());
newGeometry.setBottom(top);
}
m_selection->setGeometry(newGeometry);
m_context.selection = extendedRect(&newGeometry);
updateSizeIndicator();
m_buttonHandler->updatePosition(newGeometry);
m_buttonHandler->show();
}
m_mouseIsClicked = false;
m_newSelection = false;
m_grabbing = false;
updateCursor();
}
void CaptureWidget::keyPressEvent(QKeyEvent *e) {
if (!m_selection->isVisible()) {
return;
} else if (e->key() == Qt::Key_Up
&& m_selection->geometry().top() > rect().top()) {
m_selection->move(QPoint(m_selection->x(), m_selection->y() -1));
QRect newGeometry = m_selection->geometry().intersected(rect());
m_context.selection = extendedRect(&newGeometry);
m_buttonHandler->updatePosition(m_selection->geometry());
update();
} else if (e->key() == Qt::Key_Down
&& m_selection->geometry().bottom() < rect().bottom()) {
m_selection->move(QPoint(m_selection->x(), m_selection->y() +1));
QRect newGeometry = m_selection->geometry().intersected(rect());
m_context.selection = extendedRect(&newGeometry);
m_buttonHandler->updatePosition(m_selection->geometry());
update();
} else if (e->key() == Qt::Key_Left
&& m_selection->geometry().left() > rect().left()) {
m_selection->move(QPoint(m_selection->x() -1, m_selection->y()));
m_buttonHandler->updatePosition(m_selection->geometry());
update();
} else if (e->key() == Qt::Key_Right
&& m_selection->geometry().right() < rect().right()) {
m_selection->move(QPoint(m_selection->x() +1, m_selection->y()));
QRect newGeometry = m_selection->geometry().intersected(rect());
m_context.selection = extendedRect(&newGeometry);
m_buttonHandler->updatePosition(m_selection->geometry());
update();
}
}
void CaptureWidget::wheelEvent(QWheelEvent *e) {
m_context.thickness += e->delta() / 120;
m_context.thickness = qBound(0, m_context.thickness, 100);
QPoint topLeft = qApp->desktop()->screenGeometry(
qApp->desktop()->screenNumber(QCursor::pos())).topLeft();
int offset = m_notifierBox->width() / 4;
m_notifierBox->move(mapFromGlobal(topLeft) + QPoint(offset, offset));
m_notifierBox->showMessage(QString::number(m_context.thickness));
if (m_activeButton && m_activeButton->tool()->showMousePreview()) {
update();
}
emit thicknessChanged(m_context.thickness);
}
void CaptureWidget::resizeEvent(QResizeEvent *e) {
QWidget::resizeEvent(e);
m_context.widgetDimensions = rect();
m_context.widgetOffset = mapToGlobal(QPoint(0,0));
m_panel->setFixedHeight(height());
if (!m_context.fullscreen) {
m_buttonHandler->updateScreenRegions(rect());
}
}
void CaptureWidget::moveEvent(QMoveEvent *e) {
QWidget::moveEvent(e);
m_context.widgetOffset = mapToGlobal(QPoint(0,0));
}
void CaptureWidget::initContext(const QString &savePath, bool fullscreen) {
m_context.widgetDimensions = rect();
m_context.color = m_config.drawColorValue();
m_context.savePath = savePath;
m_context.widgetOffset = mapToGlobal(QPoint(0,0));
m_context.mousePos= mapFromGlobal(QCursor::pos());
m_context.thickness = m_config.drawThicknessValue();
m_context.fullscreen = fullscreen;
}
void CaptureWidget::initPanel() {
m_panel = new UtilityPanel(this);
makeChild(m_panel);
QRect panelRect = rect();
if (m_context.fullscreen) {
panelRect = QGuiApplication::primaryScreen()->geometry();
}
panelRect.moveTo(mapFromGlobal(panelRect.topLeft()));
panelRect.setWidth(m_colorPicker->width() * 3);
m_panel->setGeometry(panelRect);
SidePanelWidget *sidePanel =
new SidePanelWidget(&m_context.screenshot);
connect(sidePanel, &SidePanelWidget::colorChanged,
this, &CaptureWidget::setDrawColor);
connect(sidePanel, &SidePanelWidget::thicknessChanged,
this, &CaptureWidget::setDrawThickness);
connect(this, &CaptureWidget::colorChanged,
sidePanel, &SidePanelWidget::updateColor);
connect(this, &CaptureWidget::thicknessChanged,
sidePanel, &SidePanelWidget::updateThickness);
connect(sidePanel, &SidePanelWidget::togglePanel,
m_panel, &UtilityPanel::toggle);
sidePanel->colorChanged(m_context.color);
sidePanel->thicknessChanged(m_context.thickness);
m_panel->pushWidget(sidePanel);
m_panel->pushWidget(new QUndoView(&m_undoStack, this));
}
void CaptureWidget::initSelection() {
m_selection = new SelectionWidget(m_uiColor, this);
connect(m_selection, &SelectionWidget::animationEnded, this, [this](){
this->m_buttonHandler->updatePosition(this->m_selection->geometry());
});
m_selection->setVisible(false);
m_selection->setGeometry(QRect());
}
void CaptureWidget::setState(CaptureButton *b) {
if (!b) {
return;
}
if (m_toolWidget) {
m_toolWidget->deleteLater();
if (m_activeTool->isValid()) {
pushToolToStack();
}
}
if (m_activeButton != b) {
processTool(b->tool());
}
// Only close activated from button
if (b->tool()->closeOnButtonPressed()) {
close();
}
if (b->tool()->isSelectable()) {
if (m_activeButton != b) {
QWidget *confW = b->tool()->configurationWidget();
m_panel->addToolWidget(confW);
if (m_activeButton) {
m_activeButton->setColor(m_uiColor);
}
m_activeButton = b;
m_activeButton->setColor(m_contrastUiColor);
} else if (m_activeButton) {
m_panel->clearToolWidget();
m_activeButton->setColor(m_uiColor);
m_activeButton = nullptr;
}
update(); // clear mouse preview
}
}
void CaptureWidget::processTool(CaptureTool *t) {
auto backup = m_activeTool;
// The tool is active during the pressed().
m_activeTool = t;
t->pressed(m_context);
m_activeTool = backup;
}
void CaptureWidget::handleButtonSignal(CaptureTool::Request r) {
switch (r) {
case CaptureTool::REQ_CLEAR_MODIFICATIONS:
m_undoStack.setIndex(0);
update();
break;
case CaptureTool::REQ_CLOSE_GUI:
close();
break;
case CaptureTool::REQ_HIDE_GUI:
hide();
break;
case CaptureTool::REQ_HIDE_SELECTION:
m_newSelection = true;
m_selection->setVisible(false);
updateCursor();
break;
case CaptureTool::REQ_SELECT_ALL:
m_selection->setGeometryAnimated(rect());
break;
case CaptureTool::REQ_UNDO_MODIFICATION:
m_undoStack.undo();
break;
case CaptureTool::REQ_REDO_MODIFICATION:
m_undoStack.redo();
break;
case CaptureTool::REQ_REDRAW:
update();
break;
case CaptureTool::REQ_TOGGLE_SIDEBAR:
m_panel->toggle();
break;
case CaptureTool::REQ_SHOW_COLOR_PICKER:
// TODO
break;
case CaptureTool::REQ_MOVE_MODE:
setState(m_activeButton); // Disable the actual button
break;
case CaptureTool::REQ_CAPTURE_DONE_OK:
m_captureDone = true;
break;
case CaptureTool::REQ_ADD_CHILD_WIDGET:
if (!m_activeTool) {
break;
}
if (m_toolWidget) {
m_toolWidget->deleteLater();
}
m_toolWidget = m_activeTool->widget();
if (m_toolWidget) {
makeChild(m_toolWidget);
m_toolWidget->move(m_context.mousePos);
m_toolWidget->show();
m_toolWidget->setFocus();
}
break;
case CaptureTool::REQ_ADD_CHILD_WINDOW:
if (!m_activeTool) {
break;
} else {
QWidget *w = m_activeTool->widget();
connect(this, &CaptureWidget::destroyed, w, &QWidget::deleteLater);
w->show();
}
break;
case CaptureTool::REQ_ADD_EXTERNAL_WIDGETS:
if (!m_activeTool) {
break;
} else {
QWidget *w = m_activeTool->widget();
w->setAttribute(Qt::WA_DeleteOnClose);
w->show();
}
break;
default:
break;
}
}
void CaptureWidget::setDrawColor(const QColor &c) {
m_context.color = c;
ConfigHandler().setDrawColor(m_context.color);
emit colorChanged(c);
}
void CaptureWidget::setDrawThickness(const int &t)
{
m_context.thickness = qBound(0, t, 100);
ConfigHandler().setdrawThickness(m_context.thickness);
emit thicknessChanged(m_context.thickness);
}
void CaptureWidget::leftResize() {
if (m_selection->isVisible() && m_selection->geometry().right() > m_selection->geometry().left()) {
m_selection->setGeometry(m_selection->geometry() + QMargins(0, 0, -1, 0));
QRect newGeometry = m_selection->geometry().intersected(rect());
m_context.selection = extendedRect(&newGeometry);
m_buttonHandler->updatePosition(m_selection->geometry());
updateSizeIndicator();
update();
}
}
void CaptureWidget::rightResize() {
if (m_selection->isVisible() && m_selection->geometry().right() < rect().right()) {
m_selection->setGeometry(m_selection->geometry() + QMargins(0, 0, 1, 0));
QRect newGeometry = m_selection->geometry().intersected(rect());
m_context.selection = extendedRect(&newGeometry);
m_buttonHandler->updatePosition(m_selection->geometry());
updateSizeIndicator();
update();
}
}
void CaptureWidget::upResize() {
if (m_selection->isVisible() && m_selection->geometry().bottom() > m_selection->geometry().top()) {
m_selection->setGeometry(m_selection->geometry() + QMargins(0, 0, 0, -1));
QRect newGeometry = m_selection->geometry().intersected(rect());
m_context.selection = extendedRect(&newGeometry);
m_buttonHandler->updatePosition(m_selection->geometry());
updateSizeIndicator();
update();
}
}
void CaptureWidget::downResize() {
if (m_selection->isVisible() && m_selection->geometry().bottom() < rect().bottom()) {
m_selection->setGeometry(m_selection->geometry() + QMargins(0, 0, 0, 1));
QRect newGeometry = m_selection->geometry().intersected(rect());
m_context.selection = extendedRect(&newGeometry);
m_buttonHandler->updatePosition(m_selection->geometry());
updateSizeIndicator();
update();
}
}
void CaptureWidget::initShortcuts() {
new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this, SLOT(close()));
new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_S), this, SLOT(saveScreenshot()));
new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_C), this, SLOT(copyScreenshot()));
new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Z), this, SLOT(undo()));
new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Z), this, SLOT(redo()));
new QShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Right), this, SLOT(rightResize()));
new QShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Left), this, SLOT(leftResize()));
new QShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Up), this, SLOT(upResize()));
new QShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Down), this, SLOT(downResize()));
new QShortcut(Qt::Key_Space, this, SLOT(togglePanel()));
new QShortcut(Qt::Key_Escape, this, SLOT(deleteToolwidgetOrClose()));
new QShortcut(Qt::Key_Return, this, SLOT(copyScreenshot()));
new QShortcut(Qt::Key_Enter, this, SLOT(copyScreenshot()));
}
void CaptureWidget::updateSizeIndicator() {
if (m_sizeIndButton){
const QRect &selection = extendedSelection();
m_sizeIndButton->setText(QStringLiteral("%1\n%2")
.arg(selection.width())
.arg(selection.height()));
}
}
void CaptureWidget::updateCursor() {
if (m_rightClick) {
setCursor(Qt::ArrowCursor);
} else if (m_grabbing) {
setCursor(Qt::ClosedHandCursor);
} else if (!m_activeButton) {
using sw = SelectionWidget;
if (m_mouseOverHandle != sw::NO_SIDE){
// cursor on the handlers
switch (m_mouseOverHandle) {
case sw::TOPLEFT_SIDE: case sw::BOTTONRIGHT_SIDE:
setCursor(Qt::SizeFDiagCursor);
break;
case sw::TOPRIGHT_SIDE: case sw::BOTTONLEFT_SIDE:
setCursor(Qt::SizeBDiagCursor);
break;
case sw::LEFT_SIDE: case sw::RIGHT_SIDE:
setCursor(Qt::SizeHorCursor);
break;
case sw::TOP_SIDE: case sw::BOTTON_SIDE:
setCursor(Qt::SizeVerCursor);
break;
default:
break;
}
} else if (m_selection->isVisible() &&
m_selection->geometry().contains(m_context.mousePos))
{
setCursor(Qt::OpenHandCursor);
} else {
setCursor(Qt::CrossCursor);
}
} else {
setCursor(Qt::CrossCursor);
}
}
void CaptureWidget::pushToolToStack() {
auto mod = new ModificationCommand(
&m_context.screenshot, m_activeTool);
disconnect(this, &CaptureWidget::colorChanged,
m_activeTool, &CaptureTool::colorChanged);
disconnect(this, &CaptureWidget::thicknessChanged,
m_activeTool, &CaptureTool::thicknessChanged);
if (m_panel->toolWidget()) {
disconnect(m_panel->toolWidget(), nullptr, m_activeTool, nullptr);
}
m_undoStack.push(mod);
m_activeTool = nullptr;
}
void CaptureWidget::makeChild(QWidget *w) {
w->setParent(this);
w->installEventFilter(m_eventFilter);
}
void CaptureWidget::togglePanel() {
m_panel->toggle();
}
void CaptureWidget::childEnter() {
m_previewEnabled = false;
update();
}
void CaptureWidget::childLeave() {
m_previewEnabled = true;
update();
}
void CaptureWidget::copyScreenshot() {
m_captureDone = true;
ScreenshotSaver().saveToClipboard(pixmap());
close();
}
void CaptureWidget::saveScreenshot() {
m_captureDone = true;
hide();
if (m_context.savePath.isEmpty()) {
ScreenshotSaver().saveToFilesystemGUI(pixmap());
} else {
ScreenshotSaver().saveToFilesystem(pixmap(), m_context.savePath);
}
close();
}
void CaptureWidget::undo() {
m_undoStack.undo();
}
void CaptureWidget::redo() {
m_undoStack.redo();
}
QRect CaptureWidget::extendedSelection() const {
if (!m_selection->isVisible())
return QRect();
QRect r = m_selection->geometry();
return extendedRect(&r);
}
QRect CaptureWidget::extendedRect(QRect *r) const {
auto devicePixelRatio = m_context.screenshot.devicePixelRatio();
return QRect(r->left() * devicePixelRatio,
r->top() * devicePixelRatio,
r->width() * devicePixelRatio,
r->height() * devicePixelRatio);
}
|