File indexing completed on 2024-05-12 04:57:49

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2014  David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "squeezelabelv2.h"
0019 
0020 #include <QApplication>
0021 #include <QClipboard>
0022 #include <QKeyEvent>
0023 #include <QMimeData>
0024 #include <QDrag>
0025 #include <QMenu>
0026 
0027 SqueezeLabelV2::SqueezeLabelV2(QWidget* parent)
0028     : QLabel(parent)
0029 {
0030 }
0031 
0032 SqueezeLabelV2::SqueezeLabelV2(const QString &string)
0033     : QLabel()
0034 {
0035     setText(string);
0036 }
0037 
0038 void SqueezeLabelV2::setText(const QString &txt)
0039 {
0040     m_originalText = txt;
0041     QFontMetrics fm = fontMetrics();
0042     QString elided = fm.elidedText(m_originalText, Qt::ElideMiddle, width());
0043     QLabel::setText(elided);
0044 }
0045 
0046 void SqueezeLabelV2::copy()
0047 {
0048     if (selectedText().length() == text().length()) {
0049         QApplication::clipboard()->setText(m_originalText);
0050     }
0051     else {
0052         QApplication::clipboard()->setText(selectedText());
0053     }
0054 }
0055 
0056 void SqueezeLabelV2::contextMenuEvent(QContextMenuEvent* event)
0057 {
0058     if (!(textInteractionFlags() & Qt::TextSelectableByMouse) && !(textInteractionFlags() & Qt::TextSelectableByKeyboard)) {
0059         event->ignore();
0060         return;
0061     }
0062 
0063     QMenu menu;
0064     QAction* act = menu.addAction(tr("Copy"), this, &SqueezeLabelV2::copy);
0065     act->setShortcut(QKeySequence(QSL("Ctrl+C")));
0066     act->setEnabled(hasSelectedText());
0067 
0068     menu.exec(event->globalPos());
0069 }
0070 
0071 void SqueezeLabelV2::keyPressEvent(QKeyEvent* event)
0072 {
0073     if (event->key() == Qt::Key_C && event->modifiers() == Qt::ControlModifier) {
0074         copy();
0075     }
0076 }
0077 
0078 QString SqueezeLabelV2::originalText()
0079 {
0080     return m_originalText;
0081 }
0082 
0083 void SqueezeLabelV2::resizeEvent(QResizeEvent* event)
0084 {
0085     QLabel::resizeEvent(event);
0086     QFontMetrics fm = fontMetrics();
0087     QString elided = fm.elidedText(m_originalText, Qt::ElideMiddle, width());
0088     QLabel::setText(elided);
0089 }
0090 
0091 void SqueezeLabelV2::mousePressEvent(QMouseEvent* event)
0092 {
0093     if (event->buttons() & Qt::LeftButton) {
0094         m_dragStart = event->position().toPoint();
0095     }
0096 
0097     QLabel::mousePressEvent(event);
0098 }
0099 
0100 void SqueezeLabelV2::mouseMoveEvent(QMouseEvent* event)
0101 {
0102     if (!(event->buttons() & Qt::LeftButton) || selectedText().length() != text().length()) {
0103         QLabel::mouseMoveEvent(event);
0104         return;
0105     }
0106 
0107     int manhattanLength = (event->position().toPoint() - m_dragStart).manhattanLength();
0108     if (manhattanLength <= QApplication::startDragDistance()) {
0109         return;
0110     }
0111 
0112     auto* drag = new QDrag(this);
0113     auto* mime = new QMimeData;
0114     mime->setText(m_originalText);
0115 
0116     drag->setMimeData(mime);
0117     drag->exec();
0118 }