1 /*
2 
3  Copyright (c) 2003-2013 uim Project https://github.com/uim/uim
4 
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without
8  modification, are permitted provided that the following conditions
9  are met:
10 
11  1. Redistributions of source code must retain the above copyright
12     notice, this list of conditions and the following disclaimer.
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16  3. Neither the name of authors nor the names of its contributors
17     may be used to endorse or promote products derived from this software
18     without specific prior written permission.
19 
20  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
21  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
24  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  SUCH DAMAGE.
31 
32 */
33 #include "common-quimhelpertoolbar.h"
34 #include "common-uimstateindicator.h"
35 
36 #include <config.h>
37 
38 #include <QtCore/QProcess>
39 #include <QtGui/QContextMenuEvent>
40 #if QT_VERSION < 0x050000
41 # include <QtGui/QHBoxLayout>
42 # include <QtGui/QMenu>
43 # include <QtGui/QMessageBox>
44 # include <QtGui/QToolButton>
45 #else
46 # include <QtWidgets/QHBoxLayout>
47 # include <QtWidgets/QMenu>
48 # include <QtWidgets/QMessageBox>
49 # include <QtWidgets/QToolButton>
50 #endif
51 
52 #include "uim/uim-scm.h"
53 #include "qtgettext.h"
54 
55 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
56 #define UIM_ADD_QT_VERSION(command) command "-qt5"
57 #else
58 #define UIM_ADD_QT_VERSION(command) command "-qt4"
59 #endif
60 
launchHelperApplication(const QString & command)61 static void launchHelperApplication( const QString &command )
62 {
63     if ( !command.isEmpty() && !QProcess::startDetached( command ) ) {
64         QMessageBox::warning( 0, "uim",
65             _( "Cannot launch '%1'." ).arg( command ) );
66     }
67 }
68 
QUimHelperToolbar(QWidget * parent,bool isApplet)69 QUimHelperToolbar::QUimHelperToolbar( QWidget *parent, bool isApplet )
70     : QFrame( parent )
71 {
72     m_layout = new QHBoxLayout;
73     m_layout->setMargin( 0 );
74     m_layout->setSpacing( 0 );
75 
76     m_indicator = new UimStateIndicator( this );
77     m_layout->addWidget( m_indicator );
78 
79     connect( m_indicator, SIGNAL( indicatorResized() ),
80         this, SLOT( slotIndicatorResized() ) );
81     connect( m_indicator, SIGNAL( menuRequested( QMenu* ) ),
82         this, SIGNAL( menuRequested( QMenu* ) ) );
83 
84     const QString ICONDIR = UIM_PIXMAPSDIR;
85     const QString ACTION_ICONDIR = KDE4_ICONDIR "/oxygen/base/16x16/actions";
86     const QSize size( ICON_SIZE, ICON_SIZE );
87     m_swicon = QPixmap( ICONDIR + "/im_switcher.png" ).scaled(
88         size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
89     m_preficon = QPixmap( ACTION_ICONDIR + "/configure.png" ).scaled(
90         size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
91     m_dicticon = QPixmap( ICONDIR + "/uim-dict.png" ).scaled(
92         size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
93     m_padicon = QPixmap( ACTION_ICONDIR + "/format-text-bold.png" ).scaled(
94         size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
95     m_handicon = QPixmap( ACTION_ICONDIR + "/document-edit.png" ).scaled(
96         size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
97     m_helpicon = QPixmap( ACTION_ICONDIR + "/help-contents.png" ).scaled(
98         size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
99     QPixmap exiticon = QPixmap( ACTION_ICONDIR + "/window-close.png" ).scaled(
100         size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
101 
102     m_contextMenu = new QMenu( isApplet ? 0 : this );
103     m_contextMenu->addAction( m_swicon, _("Switch input method"),
104         this, SLOT(slotExecImSwitcher()) );
105     m_contextMenu->addAction( m_preficon, _("Preference"),
106          this, SLOT(slotExecPref()) );
107     m_contextMenu->addAction( m_dicticon, _("Japanese dictionary editor"),
108          this, SLOT(slotExecDict()) );
109     m_contextMenu->addAction( m_padicon, _("Input pad"),
110          this, SLOT(slotExecInputPad()) );
111     m_contextMenu->addAction( m_handicon, _("Handwriting input pad"),
112          this, SLOT(slotExecHandwritingInputPad()) );
113     m_contextMenu->addAction( m_helpicon, _("Help"),
114          this, SLOT(slotExecHelp()) );
115     if ( !isApplet )
116         m_contextMenu->addAction( exiticon, _("Quit this toolbar"),
117             this, SIGNAL(quitToolbar()) );
118     m_nr_exec_buttons = 0;
119 
120     // toolbar buttons
121     addExecImSwitcherButton();
122     addExecPrefButton();
123     addExecDictButton();
124     addExecInputPadButton();
125     addExecHandwritingInputPadButton();
126     addExecHelpButton();
127 
128     setLayout(m_layout);
129 }
130 
~QUimHelperToolbar()131 QUimHelperToolbar::~QUimHelperToolbar()
132 {
133 }
134 
getNumButtons()135 int QUimHelperToolbar::getNumButtons()
136 {
137     return m_indicator->getNumButtons() + m_nr_exec_buttons;
138 }
139 
contextMenuEvent(QContextMenuEvent * e)140 void QUimHelperToolbar::contextMenuEvent( QContextMenuEvent * e )
141 {
142     if( m_contextMenu->isHidden() )
143     {
144 #ifdef PLASMA_APPLET_UIM
145         Q_UNUSED( e );
146         emit menuRequested( m_contextMenu );
147 #else
148         m_contextMenu->move( e->globalPos() );
149         m_contextMenu->exec();
150 #endif
151     }
152 }
153 
154 QMenu *
contextMenu()155 QUimHelperToolbar::contextMenu()
156 {
157     return m_contextMenu;
158 }
159 
slotIndicatorResized()160 void QUimHelperToolbar::slotIndicatorResized()
161 {
162     emit toolbarResized();
163 }
164 
addExecImSwitcherButton()165 void QUimHelperToolbar::addExecImSwitcherButton()
166 {
167     uim_bool isShowSwitcher = uim_scm_symbol_value_bool("toolbar-show-switcher-button?");
168     if( isShowSwitcher == UIM_FALSE )
169         return;
170 
171     QToolButton * swButton = new QHelperToolbarButton( this );
172     m_layout->addWidget( swButton );
173     if( !m_swicon.isNull() )
174         swButton->setIcon( QIcon( m_swicon ) );
175     else
176         swButton->setText( "Sw" );
177 
178     connect( swButton, SIGNAL( clicked() ),
179                       this, SLOT( slotExecImSwitcher() ) );
180     swButton->setToolTip( _( "Switch input method" ) );
181     ++m_nr_exec_buttons;
182 }
183 
184 
slotExecImSwitcher()185 void QUimHelperToolbar::slotExecImSwitcher()
186 {
187     /* exec uim-im-switcher */
188     launchHelperApplication( UIM_ADD_QT_VERSION("uim-im-switcher") );
189 }
190 
addExecPrefButton()191 void QUimHelperToolbar::addExecPrefButton()
192 {
193     uim_bool isShowPref = uim_scm_symbol_value_bool("toolbar-show-pref-button?");
194     if( isShowPref == UIM_FALSE )
195         return;
196 
197     QToolButton * prefButton = new QHelperToolbarButton( this );
198     m_layout->addWidget( prefButton );
199     if( !m_preficon.isNull() )
200         prefButton->setIcon( QIcon( m_preficon ) );
201     else
202         prefButton->setText( "Pref" );
203 
204     connect( prefButton, SIGNAL( clicked() ),
205                       this, SLOT( slotExecPref() ) );
206     prefButton->setToolTip( _( "Preference" ) );
207     ++m_nr_exec_buttons;
208 }
209 
slotExecPref()210 void QUimHelperToolbar::slotExecPref()
211 {
212     /* exec uim-pref-qt4 */
213     launchHelperApplication( UIM_ADD_QT_VERSION("uim-pref") );
214 }
215 
addExecDictButton()216 void QUimHelperToolbar::addExecDictButton()
217 {
218     uim_bool isShowDict = uim_scm_symbol_value_bool("toolbar-show-dict-button?");
219     if( isShowDict == UIM_FALSE )
220         return;
221 
222     QToolButton *dictButton = new QHelperToolbarButton( this );
223     m_layout->addWidget( dictButton );
224     if( !m_dicticon.isNull() )
225         dictButton->setIcon( QIcon( m_dicticon ) );
226     else
227         dictButton->setText( "Dic" );
228 
229     connect( dictButton, SIGNAL( clicked() ),
230                       this, SLOT( slotExecDict() ) );
231     dictButton->setToolTip( _( "Japanese dictionary editor" ) );
232     ++m_nr_exec_buttons;
233 }
234 
slotExecDict()235 void QUimHelperToolbar::slotExecDict()
236 {
237     /* exec uim-dict */
238     launchHelperApplication( "uim-dict-gtk" );
239 }
240 
addExecInputPadButton()241 void QUimHelperToolbar::addExecInputPadButton()
242 {
243     uim_bool isShowInputPad = uim_scm_symbol_value_bool("toolbar-show-input-pad-button?");
244     if( isShowInputPad == UIM_FALSE )
245         return;
246 
247     QToolButton *inputpadButton = new QHelperToolbarButton( this );
248     m_layout->addWidget( inputpadButton );
249     if( !m_padicon.isNull() )
250         inputpadButton->setIcon( QIcon( m_padicon ) );
251     else
252         inputpadButton->setText( "Pad" );
253 
254     connect( inputpadButton, SIGNAL( clicked() ),
255                       this, SLOT( slotExecInputPad() ) );
256     inputpadButton->setToolTip( _( "Input pad" ) );
257     ++m_nr_exec_buttons;
258 }
259 
slotExecInputPad()260 void QUimHelperToolbar::slotExecInputPad()
261 {
262     /* exec input pad */
263     launchHelperApplication( UIM_ADD_QT_VERSION("uim-chardict") );
264 }
265 
addExecHandwritingInputPadButton()266 void QUimHelperToolbar::addExecHandwritingInputPadButton()
267 {
268     uim_bool isShowHandwritingInputPad = uim_scm_symbol_value_bool("toolbar-show-handwriting-input-pad-button?");
269     if( isShowHandwritingInputPad == UIM_FALSE )
270         return;
271 
272     QToolButton *handwritingButton = new QHelperToolbarButton( this );
273     m_layout->addWidget( handwritingButton );
274     if( !m_handicon.isNull() )
275         handwritingButton->setIcon( QIcon( m_handicon ) );
276     else
277         handwritingButton->setText( "Hand" );
278 
279     connect( handwritingButton, SIGNAL( clicked() ),
280                       this, SLOT( slotExecHandwritingInputPad() ) );
281     handwritingButton->setToolTip( _( "Handwriting input pad" ) );
282     ++m_nr_exec_buttons;
283 }
284 
slotExecHandwritingInputPad()285 void QUimHelperToolbar::slotExecHandwritingInputPad()
286 {
287     launchHelperApplication( "uim-tomoe-gtk" );
288 }
289 
addExecHelpButton()290 void QUimHelperToolbar::addExecHelpButton()
291 {
292     uim_bool isShowHelp = uim_scm_symbol_value_bool("toolbar-show-help-button?");
293     if( isShowHelp == UIM_FALSE )
294         return;
295 
296     QToolButton *helpButton = new QHelperToolbarButton( this );
297     m_layout->addWidget( helpButton );
298     if( !m_helpicon.isNull() )
299         helpButton->setIcon( QIcon( m_helpicon ) );
300     else
301         helpButton->setText( "Help" );
302 
303     connect( helpButton, SIGNAL( clicked() ),
304                       this, SLOT( slotExecHelp() ) );
305     helpButton->setToolTip( _( "Help" ) );
306     ++m_nr_exec_buttons;
307 }
308 
slotExecHelp()309 void QUimHelperToolbar::slotExecHelp()
310 {
311     launchHelperApplication( "uim-help" );
312 }
313 
setMargin(int margin)314 void QUimHelperToolbar::setMargin( int margin )
315 {
316     m_layout->setMargin( margin );
317 }
318