1 /*
2  * Copyright (c) 2015 Friedrich W. H. Kossebau <kossebau@kde.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #include "KoToolBoxButton_p.h"
21 
22 #include <KoToolManager.h>
23 #include <QIcon>
24 #include <QPalette>
25 #include <QApplication>
26 #include <klocalizedstring.h>
27 #include <QKeySequence>
28 #include <KoIcon.h>
29 
KoToolBoxButton(KoToolAction * toolAction,QWidget * parent)30 KoToolBoxButton::KoToolBoxButton(KoToolAction *toolAction, QWidget *parent)
31     : QToolButton(parent)
32     , m_toolAction(toolAction)
33 {
34     setObjectName(m_toolAction->id());
35     // ensure same L&F
36     setCheckable(true);
37     setAutoRaise(true);
38     setIcon(kisIcon(m_toolAction->iconName()));
39 
40     setDataFromToolAction();
41 
42     connect(this, SIGNAL(clicked(bool)), m_toolAction, SLOT(trigger()));
43     connect(m_toolAction, SIGNAL(changed()), SLOT(setDataFromToolAction()));
44 }
45 
setHighlightColor()46 void KoToolBoxButton::setHighlightColor()
47 {
48     QPalette p = qApp->palette();
49     if (isChecked()) {
50         QPalette palette_highlight(p);
51         QColor c = p.color(QPalette::Highlight);
52         palette_highlight.setColor(QPalette::Button, c);
53         setPalette(palette_highlight);
54     }
55     else {
56         setPalette(p);
57     }
58 }
59 
setDataFromToolAction()60 void KoToolBoxButton::setDataFromToolAction()
61 {
62     const QString plainToolTip = m_toolAction->toolTip();
63     const QKeySequence shortcut = m_toolAction->shortcut();
64     const QString toolTip =
65         shortcut.isEmpty() ?
66             plainToolTip :
67             i18nc("@info:tooltip %2 is shortcut", "%1 (%2)", plainToolTip,
68                 shortcut.toString(QKeySequence::NativeText));
69 
70     setToolTip(toolTip);
71 }
72