aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Haumann <dhaumann@kde.org>2014-01-24 17:19:37 (GMT)
committerDominik Haumann <dhaumann@kde.org>2014-01-24 17:19:37 (GMT)
commit2ab8be72e1ee900be298709f41567f947d03f286 (patch)
tree7de68562e9cea8ad344b5c55df57d8be23c3ed8e
parent32d900bf9de6e6ff9ba791dc580fe11ef7786984 (diff)
add close buttons, better highlight for active tab
-rw-r--r--kate/src/katetabbar.cpp26
-rw-r--r--kate/src/katetabbar.h2
-rw-r--r--kate/src/katetabbutton.cpp90
-rw-r--r--kate/src/katetabbutton.h19
4 files changed, 104 insertions, 33 deletions
diff --git a/kate/src/katetabbar.cpp b/kate/src/katetabbar.cpp
index ed84ba8..4d29d77 100644
--- a/kate/src/katetabbar.cpp
+++ b/kate/src/katetabbar.cpp
@@ -130,10 +130,6 @@ int KateTabBar::insertTab(int position, const QString & text)
this, SLOT(tabButtonHighlightChanged(KateTabButton*)));
connect(tabButton, SIGNAL(closeRequest(KateTabButton*)),
this, SLOT(tabButtonCloseRequest(KateTabButton*)));
- connect(tabButton, SIGNAL(closeOtherTabsRequest(KateTabButton*)),
- this, SLOT(tabButtonCloseOtherRequest(KateTabButton*)));
- connect(tabButton, SIGNAL(closeAllTabsRequest()),
- this, SLOT(tabButtonCloseAllRequest()));
updateButtonPositions();
@@ -380,28 +376,6 @@ void KateTabBar::tabButtonCloseRequest(KateTabButton *tabButton)
}
/**
- * If the user wants to close all tabs except the current one using the context
- * menu, it sends multiple close requests.
- * Throw the close requests by emitting the signal @p closeRequest().
- */
-void KateTabBar::tabButtonCloseOtherRequest(KateTabButton *tabButton)
-{
- const int id = m_idToTab.key(tabButton, -1);
- Q_ASSERT(id >= 0);
- emit closeOtherTabsRequested(id);
-}
-
-/**
- * If the user wants to close all the tabs using the context menu, it sends
- * multiple close requests.
- * Throw the close requests by emitting the signal @p closeRequest().
- */
-void KateTabBar::tabButtonCloseAllRequest()
-{
- emit closeAllTabsRequested();
-}
-
-/**
* Recalculate geometry for all children.
*/
void KateTabBar::resizeEvent(QResizeEvent *event)
diff --git a/kate/src/katetabbar.h b/kate/src/katetabbar.h
index 6954de1..12b52bc 100644
--- a/kate/src/katetabbar.h
+++ b/kate/src/katetabbar.h
@@ -130,9 +130,7 @@ Q_SIGNALS:
protected Q_SLOTS:
void tabButtonActivated(KateTabButton *tabButton);
void tabButtonHighlightChanged(KateTabButton *tabButton);
- void tabButtonCloseAllRequest();
void tabButtonCloseRequest(KateTabButton *tabButton);
- void tabButtonCloseOtherRequest(KateTabButton *tabButton);
protected:
virtual void resizeEvent(QResizeEvent *event);
diff --git a/kate/src/katetabbutton.cpp b/kate/src/katetabbutton.cpp
index 0f6e47d..f30e474 100644
--- a/kate/src/katetabbutton.cpp
+++ b/kate/src/katetabbutton.cpp
@@ -30,6 +30,65 @@
#include <QMenu>
#include <QPainter>
#include <QStyle>
+#include <QStyleOption>
+#include <QHBoxLayout>
+
+TabCloseButton::TabCloseButton(QWidget * parent)
+ : QAbstractButton(parent)
+{
+ // should never have focus
+ setFocusPolicy(Qt::NoFocus);
+
+ // closing a tab closes the document
+ setToolTip(i18n("Close Document"));
+}
+
+void TabCloseButton::paintEvent(QPaintEvent *event)
+{
+ Q_UNUSED(event)
+
+ // get the tab this close button belongs to
+ KateTabButton *tabButton = qobject_cast<KateTabButton*>(parent());
+ const bool isActive = underMouse()
+ || (tabButton && tabButton->isChecked());
+
+ // set style options depending on current state
+ QStyleOption opt;
+ opt.init(this);
+ if (isActive && !isDown()) {
+ opt.state |= QStyle::State_Raised;
+ }
+ if (isDown()) {
+ opt.state |= QStyle::State_Sunken;
+ }
+
+ QPainter p(this);
+ style()->drawPrimitive(QStyle::PE_IndicatorTabClose, &opt, &p, this);
+}
+
+QSize TabCloseButton::sizeHint() const
+{
+ // make sure the widget is polished
+ ensurePolished();
+
+ // read the metrics from the style
+ const int w = style()->pixelMetric(QStyle::PM_TabCloseIndicatorWidth, 0, this);
+ const int h = style()->pixelMetric(QStyle::PM_TabCloseIndicatorHeight, 0, this);
+ return QSize(w, h);
+}
+
+void TabCloseButton::enterEvent(QEvent *event)
+{
+ update(); // repaint on hover
+ QAbstractButton::enterEvent(event);
+}
+
+void TabCloseButton::leaveEvent(QEvent *event)
+{
+ update(); // repaint on hover
+ QAbstractButton::leaveEvent(event);
+}
+
QColor KateTabButton::s_predefinedColors[] = { Qt::red, Qt::yellow, Qt::green, Qt::cyan, Qt::blue, Qt::magenta };
const int KateTabButton::s_colorCount = 6;
@@ -50,6 +109,17 @@ KateTabButton::KateTabButton(const QString &caption, QWidget *parent)
setText(caption);
connect(this, SIGNAL(clicked()), this, SLOT(buttonClicked()));
+
+ // add close button
+ const int margin = style()->pixelMetric(QStyle::PM_ButtonMargin, 0, this);
+ m_closeButton = new TabCloseButton(this);
+ QHBoxLayout * hbox = new QHBoxLayout(this);
+ hbox->setSpacing(0);
+ hbox->setContentsMargins(0, 0, margin, 0);
+ hbox->addStretch();
+ hbox->addWidget(m_closeButton);
+ setLayout(hbox);
+ connect(m_closeButton, SIGNAL(clicked()), this, SLOT(closeButtonClicked()));
}
KateTabButton::~KateTabButton()
@@ -66,6 +136,11 @@ void KateTabButton::buttonClicked()
}
}
+void KateTabButton::closeButtonClicked()
+{
+ emit closeRequest(this);
+}
+
void KateTabButton::setActivated(bool active)
{
if (isChecked() == active) {
@@ -87,15 +162,11 @@ void KateTabButton::paintEvent(QPaintEvent *ev)
QPalette pal = QApplication::palette();
QPainter p(this);
- if (underMouse()) {
+ if (isChecked() || underMouse()) {
QColor c = pal.color(QPalette::Background);
p.fillRect(rect(), c.lighter(110));
}
- // draw text, we need to elide to xxx...xxx is too long
- const QString elidedText = QFontMetrics(font()).elidedText (text(), Qt::ElideMiddle, rect().width());
- style()->drawItemText(&p, rect(), Qt::AlignHCenter | Qt::AlignVCenter, pal, true, elidedText);
-
if (m_highlightColor.isValid()) {
p.fillRect(QRect(0, height() - 3, width(), 10), m_highlightColor);
}
@@ -103,6 +174,15 @@ void KateTabButton::paintEvent(QPaintEvent *ev)
if (isActivated()) {
p.fillRect(QRect(0, height() - 3, width(), 10), QColor(0, 0, 255, 128));
}
+
+ // the width of the text is reduced by the close button + 2 * margin
+ const int margin = style()->pixelMetric(QStyle::PM_ButtonMargin, 0, this);
+ const int w = width() - m_closeButton->width() - 2 * margin;
+
+ // draw text, we need to elide to xxx...xxx is too long
+ const QString elidedText = QFontMetrics(font()).elidedText (text(), Qt::ElideMiddle, w);
+ const QRect textRect(0, 0, w, height());
+ style()->drawItemText(&p, textRect, Qt::AlignHCenter | Qt::AlignVCenter, pal, true, elidedText);
}
void KateTabButton::contextMenuEvent(QContextMenuEvent *ev)
diff --git a/kate/src/katetabbutton.h b/kate/src/katetabbutton.h
index 8ee64bf..3dddb33 100644
--- a/kate/src/katetabbutton.h
+++ b/kate/src/katetabbutton.h
@@ -24,6 +24,23 @@
#include <QPushButton>
#include "katetabbar.h"
+class TabCloseButton : public QAbstractButton
+{
+ Q_OBJECT
+
+public:
+ // constructor
+ TabCloseButton(QWidget * parent = 0);
+ // paint close button depending on its state
+ void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
+ // returns the size hint depending on the style
+ QSize sizeHint() const Q_DECL_OVERRIDE;
+
+protected:
+ void enterEvent(QEvent *event) Q_DECL_OVERRIDE;
+ void leaveEvent(QEvent *event) Q_DECL_OVERRIDE;
+};
+
/**
* A \p KateTabButton represents a button on the tab bar. It can either be
* \e activated or \e deactivated. If the state is \e deactivated it will
@@ -97,6 +114,7 @@ Q_SIGNALS:
protected Q_SLOTS:
void buttonClicked();
+ void closeButtonClicked();
protected:
/** paint eyecandy rectangles around the button */
@@ -107,6 +125,7 @@ protected:
virtual void mousePressEvent(QMouseEvent *ev);
private:
+ TabCloseButton * m_closeButton;
bool m_modified;
QColor m_highlightColor;