Commit 2ab8be72 authored by Dominik Haumann's avatar Dominik Haumann

add close buttons, better highlight for active tab

parent 32d900bf
......@@ -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();
......@@ -379,28 +375,6 @@ void KateTabBar::tabButtonCloseRequest(KateTabButton *tabButton)
emit closeTabRequested(id);
}
/**
* 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.
*/
......
......@@ -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);
......
......@@ -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)
......
......@@ -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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment