Falkon Develop
Cross-platform Qt-based web browser
squeezelabelv2.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "squeezelabelv2.h"
19
20#include <QApplication>
21#include <QClipboard>
22#include <QKeyEvent>
23#include <QMimeData>
24#include <QDrag>
25#include <QMenu>
26
28 : QLabel(parent)
29{
30}
31
32SqueezeLabelV2::SqueezeLabelV2(const QString &string)
33 : QLabel()
34{
35 setText(string);
36}
37
38void SqueezeLabelV2::setText(const QString &txt)
39{
40 m_originalText = txt;
41 QFontMetrics fm = fontMetrics();
42 QString elided = fm.elidedText(m_originalText, Qt::ElideMiddle, width());
43 QLabel::setText(elided);
44}
45
46void SqueezeLabelV2::copy()
47{
48 if (selectedText().length() == text().length()) {
49 QApplication::clipboard()->setText(m_originalText);
50 }
51 else {
52 QApplication::clipboard()->setText(selectedText());
53 }
54}
55
56void SqueezeLabelV2::contextMenuEvent(QContextMenuEvent* event)
57{
58 if (!(textInteractionFlags() & Qt::TextSelectableByMouse) && !(textInteractionFlags() & Qt::TextSelectableByKeyboard)) {
59 event->ignore();
60 return;
61 }
62
63 QMenu menu;
64 QAction* act = menu.addAction(tr("Copy"), this, &SqueezeLabelV2::copy);
65 act->setShortcut(QKeySequence(QSL("Ctrl+C")));
66 act->setEnabled(hasSelectedText());
67
68 menu.exec(event->globalPos());
69}
70
71void SqueezeLabelV2::keyPressEvent(QKeyEvent* event)
72{
73 if (event->key() == Qt::Key_C && event->modifiers() == Qt::ControlModifier) {
74 copy();
75 }
76}
77
79{
80 return m_originalText;
81}
82
83void SqueezeLabelV2::resizeEvent(QResizeEvent* event)
84{
85 QLabel::resizeEvent(event);
86 QFontMetrics fm = fontMetrics();
87 QString elided = fm.elidedText(m_originalText, Qt::ElideMiddle, width());
88 QLabel::setText(elided);
89}
90
91void SqueezeLabelV2::mousePressEvent(QMouseEvent* event)
92{
93 if (event->buttons() & Qt::LeftButton) {
94 m_dragStart = event->position().toPoint();
95 }
96
97 QLabel::mousePressEvent(event);
98}
99
100void SqueezeLabelV2::mouseMoveEvent(QMouseEvent* event)
101{
102 if (!(event->buttons() & Qt::LeftButton) || selectedText().length() != text().length()) {
103 QLabel::mouseMoveEvent(event);
104 return;
105 }
106
107 int manhattanLength = (event->position().toPoint() - m_dragStart).manhattanLength();
108 if (manhattanLength <= QApplication::startDragDistance()) {
109 return;
110 }
111
112 auto* drag = new QDrag(this);
113 auto* mime = new QMimeData;
114 mime->setText(m_originalText);
115
116 drag->setMimeData(mime);
117 drag->exec();
118}
void resizeEvent(QResizeEvent *event) override
void keyPressEvent(QKeyEvent *event) override
void setText(const QString &txt)
void mouseMoveEvent(QMouseEvent *event) override
void mousePressEvent(QMouseEvent *event) override
SqueezeLabelV2(QWidget *parent=nullptr)
QString originalText()
void contextMenuEvent(QContextMenuEvent *event) override
#define QSL(x)
Definition: qzcommon.h:40