From: Marcus Eggenberger Date: Wed, 2 Jul 2008 14:14:26 +0000 (+0200) Subject: Fixing Topic Widget (vertical centering, scrolling and doubleclickination) X-Git-Tag: 0.3.0~322 X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=commitdiff_plain;h=982ece4929696e3817875543cab527740cc7e023 Fixing Topic Widget (vertical centering, scrolling and doubleclickination) --- diff --git a/src/qtui/CMakeLists.txt b/src/qtui/CMakeLists.txt index 561b46e7..1bb30fea 100644 --- a/src/qtui/CMakeLists.txt +++ b/src/qtui/CMakeLists.txt @@ -21,7 +21,7 @@ set(SOURCES settingsdlg.cpp settingspagedlg.cpp titlesetter.cpp - topicbutton.cpp + topiclabel.cpp topicwidget.cpp verticaldock.cpp) @@ -40,7 +40,7 @@ set(MOC_HDRS settingsdlg.h settingspagedlg.h titlesetter.h - topicbutton.h + topiclabel.h topicwidget.h verticaldock.h) diff --git a/src/qtui/topicbutton.cpp b/src/qtui/topicbutton.cpp deleted file mode 100644 index f2f60af1..00000000 --- a/src/qtui/topicbutton.cpp +++ /dev/null @@ -1,86 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2005/06 by the Quassel Project * - * devel@quassel-irc.org * - * * - * This program 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 2 of the License, or * - * (at your option) version 3. * - * * - * This program 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 this program; if not, write to the * - * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - ***************************************************************************/ - -#include "topicbutton.h" - - -#include - -#include -#include -#include -#include -#include - -#include "qtui.h" -#include "message.h" - -TopicButton::TopicButton(QWidget *parent) - : QAbstractButton(parent) -{ - setFixedHeight(QFontMetrics(qApp->font()).height()); - setToolTip(tr("Click to edit!")); -} - -void TopicButton::paintEvent(QPaintEvent *event) { - Q_UNUSED(event); - - QPainter painter(this); - painter.setBackgroundMode(Qt::OpaqueMode); - - // FIXME re-enable topic painting -#ifndef SPUTDEV - QRect drawRect = rect(); - QRect brect; - QString textPart; - foreach(QTextLayout::FormatRange fr, styledContents.formatList) { - textPart = styledContents.plainText.mid(fr.start, fr.length); - painter.setFont(fr.format.font()); - painter.setPen(QPen(fr.format.foreground(), 0)); - painter.setBackground(fr.format.background()); - painter.drawText(drawRect, Qt::AlignLeft|Qt::TextSingleLine, textPart, &brect); - drawRect.setLeft(brect.right()); - } -#endif -} - -void TopicButton::setAndStyleText(const QString &text) { - if(QAbstractButton::text() == text) - return; - - setText(text); // this triggers a repaint event - -#ifndef SPUTDEV - styledContents = QtUi::style()->styleString(Message::mircToInternal(text)); - int height = 1; - foreach(QTextLayout::FormatRange fr, styledContents.formatList) { - height = qMax(height, QFontMetrics(fr.format.font()).height()); - } - - // ensure the button is editable (height != 1) if there is no text to show - if(text.isEmpty()) - height = QFontMetrics(qApp->font()).height(); - - setFixedHeight(height); -#endif - // show topic in tooltip - setToolTip(tr("%1\n\nClick to edit!").arg(QAbstractButton::text())); -} - diff --git a/src/qtui/topiclabel.cpp b/src/qtui/topiclabel.cpp new file mode 100644 index 00000000..33a752c0 --- /dev/null +++ b/src/qtui/topiclabel.cpp @@ -0,0 +1,168 @@ +/*************************************************************************** + * Copyright (C) 2005/06 by the Quassel Project * + * devel@quassel-irc.org * + * * + * This program 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 2 of the License, or * + * (at your option) version 3. * + * * + * This program 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 this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include "topiclabel.h" + + +#include + +#include +#include +#include +// #include +#include +#include + +#include "qtui.h" +#include "message.h" + +TopicLabel::TopicLabel(QWidget *parent) + : QFrame(parent), + offset(0), + dragStartX(0), + dragMode(false) +{ +} + +void TopicLabel::paintEvent(QPaintEvent *event) { + Q_UNUSED(event); + + textPartOffset.clear(); + + QPainter painter(this); + painter.setBackgroundMode(Qt::OpaqueMode); + + // FIXME re-enable topic painting +#ifndef SPUTDEV + QRect drawRect = rect().adjusted(offset, 0, 0, 0); + QRect brect; + QString textPart; + foreach(QTextLayout::FormatRange fr, styledContents.formatList) { + textPart = styledContents.plainText.mid(fr.start, fr.length); + textPartOffset << drawRect.left(); + painter.setFont(fr.format.font()); + painter.setPen(QPen(fr.format.foreground(), 0)); + painter.setBackground(fr.format.background()); + painter.drawText(drawRect, Qt::AlignLeft|Qt::AlignVCenter, textPart, &brect); + drawRect.setLeft(brect.right()); + } +#endif +} + +void TopicLabel::setText(const QString &text) { + if(_text == text) + return; + + _text = text; + update(); + +#ifndef SPUTDEV + styledContents = QtUi::style()->styleString(Message::mircToInternal(text)); + int height = 1; + foreach(QTextLayout::FormatRange fr, styledContents.formatList) { + height = qMax(height, QFontMetrics(fr.format.font()).height()); + } + + // ensure the label is editable (height != 1) if there is no text to show + if(text.isEmpty()) + height = QFontMetrics(qApp->font()).height(); + + // setFixedHeight(height); +#endif + // show topic in tooltip + setToolTip(_text); +} + + +void TopicLabel::mouseMoveEvent(QMouseEvent *event) { + if(!dragMode) + return; + + event->accept(); + offset = event->pos().x() - dragStartX; + update(); +} + +void TopicLabel::mousePressEvent(QMouseEvent *event) { + event->accept(); + dragMode = true; + dragStartX = event->pos().x() - offset; +} + +void TopicLabel::mouseReleaseEvent(QMouseEvent *event) { + event->accept(); + dragMode = false; + if(qAbs(offset) < 10) { + offset = 0; + update(); + } +} + +void TopicLabel::mouseDoubleClickEvent(QMouseEvent *event) { +#ifndef SPUTDEV + event->accept(); + int textPart = 0; + int textOffset = 0; + + if(textPartOffset.isEmpty()) + return; + + // find the text part that contains the url. We don't expect color codes in urls so we expect only full parts (yet?) + int x = event->pos().x(); + while(textPart + 1 < textPartOffset.count()) { + if(textPartOffset[textPart + 1] < x) { + textPart++; + textOffset = textPartOffset[textPart]; + } else { + break; + } + } + + // we've Identified the needed text part \o/ + QString text = styledContents.plainText.mid(styledContents.formatList[textPart].start, styledContents.formatList[textPart].length); + + // now we have to find the the left and right word delimiters of the clicked word + QFontMetrics fontMetric(styledContents.formatList[textPart].format.font()); + + int start = 0; + int spacePos = text.indexOf(" "); + while(spacePos != -1) { + if(fontMetric.width(text.left(spacePos + 1)) + textOffset < x) { + start = spacePos + 1; + spacePos = text.indexOf(" ", start + 1); + } else { + break; + } + } + + int end = text.indexOf(" ", start); + int len = -1; + if(end != -1) { + len = end - start; + } + QString word = text.mid(start, len); + qDebug() << word; + QRegExp regex("^(h|f)t{1,2}ps?:\\/\\/"); + if(regex.indexIn(word) != -1) { + QDesktopServices::openUrl(QUrl(word)); + } +#endif + +} diff --git a/src/qtui/topicbutton.h b/src/qtui/topiclabel.h similarity index 77% rename from src/qtui/topicbutton.h rename to src/qtui/topiclabel.h index 71e9cd89..756c35a5 100644 --- a/src/qtui/topicbutton.h +++ b/src/qtui/topiclabel.h @@ -18,30 +18,42 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -#ifndef TOPICBUTTON_H -#define TOPICBUTTON_H +#ifndef TOPICLABEL_H +#define TOPICLABEL_H #include -#include +#include #include "uistyle.h" -class TopicButton : public QAbstractButton { +class TopicLabel : public QFrame { Q_OBJECT public: - TopicButton(QWidget *parent = 0); + TopicLabel(QWidget *parent = 0); - void setAndStyleText(const QString &text); + void setText(const QString &text); protected: virtual void paintEvent(QPaintEvent *event); + void mouseMoveEvent(QMouseEvent *event); + void mousePressEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *event); + void mouseDoubleClickEvent(QMouseEvent *event); + private: #ifndef SPUTDEV UiStyle::StyledText styledContents; #endif + QString _text; QSize _sizeHint; + + int offset; + int dragStartX; + bool dragMode; + + QList textPartOffset; // needed for location url positions }; #endif diff --git a/src/qtui/topicwidget.cpp b/src/qtui/topicwidget.cpp index cb646eda..bb55280a 100644 --- a/src/qtui/topicwidget.cpp +++ b/src/qtui/topicwidget.cpp @@ -28,7 +28,7 @@ TopicWidget::TopicWidget(QWidget *parent) ui.setupUi(this); ui.topicLineEdit->hide(); ui.topicLineEdit->installEventFilter(this); - ui.topicButton->show(); + ui.topicLabel->show(); } void TopicWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) { @@ -48,7 +48,7 @@ void TopicWidget::setTopic(const QString &newtopic) { return; _topic = newtopic; - ui.topicButton->setAndStyleText(newtopic); + ui.topicLabel->setText(newtopic); ui.topicLineEdit->setText(newtopic); switchPlain(); } @@ -58,19 +58,21 @@ void TopicWidget::on_topicLineEdit_returnPressed() { switchPlain(); } -void TopicWidget::on_topicButton_clicked() { +void TopicWidget::on_topicEditButton_clicked() { switchEditable(); } void TopicWidget::switchEditable() { - ui.topicButton->hide(); + ui.topicLabel->hide(); + ui.topicEditButton->hide(); ui.topicLineEdit->show(); ui.topicLineEdit->setFocus(); } void TopicWidget::switchPlain() { ui.topicLineEdit->hide(); - ui.topicButton->show(); + ui.topicLabel->show(); + ui.topicEditButton->show(); ui.topicLineEdit->setText(_topic); } @@ -93,3 +95,4 @@ bool TopicWidget::eventFilter(QObject *obj, QEvent *event) { return false; } + diff --git a/src/qtui/topicwidget.h b/src/qtui/topicwidget.h index 37d23214..5933d5e2 100644 --- a/src/qtui/topicwidget.h +++ b/src/qtui/topicwidget.h @@ -44,7 +44,7 @@ protected slots: private slots: void on_topicLineEdit_returnPressed(); - void on_topicButton_clicked(); + void on_topicEditButton_clicked(); void switchEditable(); void switchPlain(); diff --git a/src/qtui/ui/topicwidget.ui b/src/qtui/ui/topicwidget.ui index 8452e8d8..cb00366b 100644 --- a/src/qtui/ui/topicwidget.ui +++ b/src/qtui/ui/topicwidget.ui @@ -5,8 +5,8 @@ 0 0 - 515 - 44 + 634 + 36 @@ -31,45 +31,57 @@ Form - - 2 - - - 2 - - + 4 - - 10 - - - - - + - + 0 0 + + + + + :/22x22/actions/oxygen/22x22/actions/edit-clear.png + + + + + + + QFrame::StyledPanel + + + QFrame::Raised + + + + - - TopicButton - QWidget -
topicbutton.h
- 1 -
ClearableLineEdit QLineEdit
clearablelineedit.h
+ + ClickableLabel + QLabel +
clickablelabel.h
+
+ + TopicLabel + QFrame +
topiclabel.h
+ 1 +
diff --git a/src/uisupport/CMakeLists.txt b/src/uisupport/CMakeLists.txt index ad63477d..a070ab21 100644 --- a/src/uisupport/CMakeLists.txt +++ b/src/uisupport/CMakeLists.txt @@ -10,6 +10,7 @@ set(SOURCES bufferview.cpp bufferviewfilter.cpp clearablelineedit.cpp + clickablelabel.cpp colorbutton.cpp nickviewfilter.cpp inputline.cpp @@ -25,6 +26,7 @@ set(MOC_HDRS bufferview.h bufferviewfilter.h clearablelineedit.h + clickablelabel.h colorbutton.h nickviewfilter.h inputline.h diff --git a/src/uisupport/clickablelabel.cpp b/src/uisupport/clickablelabel.cpp new file mode 100644 index 00000000..debad83f --- /dev/null +++ b/src/uisupport/clickablelabel.cpp @@ -0,0 +1,38 @@ +/*************************************************************************** + * Copyright (C) 2005-08 by the Quassel Project * + * devel@quassel-irc.org * + * * + * This program 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 2 of the License, or * + * (at your option) version 3. * + * * + * This program 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 this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include "clickablelabel.h" + +#include +#include + +ClickableLabel::ClickableLabel(QWidget *parent) + : QLabel(parent) +{ +} + + +void ClickableLabel::mouseReleaseEvent(QMouseEvent *event) { + if(event->pos().x() > size().width() || event->pos().y() > size().height()) + return; + + event->accept(); + emit clicked(); +} diff --git a/src/uisupport/clickablelabel.h b/src/uisupport/clickablelabel.h new file mode 100644 index 00000000..11ebeb5d --- /dev/null +++ b/src/uisupport/clickablelabel.h @@ -0,0 +1,39 @@ +/*************************************************************************** + * Copyright (C) 2005-08 by the Quassel Project * + * devel@quassel-irc.org * + * * + * This program 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 2 of the License, or * + * (at your option) version 3. * + * * + * This program 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 this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#ifndef CLICKABLELABEL_H +#define CLICKABLELABEL_H + +#include + +class ClickableLabel : public QLabel { + Q_OBJECT + +public: + ClickableLabel(QWidget *parent = 0); + +signals: + void clicked(); + +protected: + void mouseReleaseEvent(QMouseEvent *event); +}; + +#endif //CLICKABLELABEL_H