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 "qt4.h"
34 #include "bushuviewwidget.h"
35 #include "unicodeviewwidget.h"
36
37 #include <QtCore/QSettings>
38 #include <QtGui/QFont>
39 #if QT_VERSION < 0x050000
40 # include <QtGui/QApplication>
41 # include <QtGui/QComboBox>
42 # include <QtGui/QFontDialog>
43 # include <QtGui/QHBoxLayout>
44 # include <QtGui/QLabel>
45 # include <QtGui/QLineEdit>
46 # include <QtGui/QPushButton>
47 # include <QtGui/QSizePolicy>
48 # include <QtGui/QVBoxLayout>
49 # include <QtGui/QStackedWidget>
50 #else
51 # include <QtWidgets/QApplication>
52 # include <QtWidgets/QComboBox>
53 # include <QtWidgets/QFontDialog>
54 # include <QtWidgets/QHBoxLayout>
55 # include <QtWidgets/QLabel>
56 # include <QtWidgets/QLineEdit>
57 # include <QtWidgets/QPushButton>
58 # include <QtWidgets/QSizePolicy>
59 # include <QtWidgets/QVBoxLayout>
60 # include <QtWidgets/QStackedWidget>
61
62 #endif
63
64 #ifdef Q_WS_X11
65 #include <QtGui/QX11Info>
66
67 #include <X11/Xutil.h>
68 #endif
69
70 #include <clocale>
71
72 #include <uim/uim.h>
73 #include <uim/uim-helper.h>
74
75 #include "qtgettext.h"
76
77 static int uim_fd = -1;
78
main(int argc,char * argv[])79 int main( int argc, char *argv[] )
80 {
81
82 setlocale(LC_ALL, "");
83 bindtextdomain(PACKAGE, LOCALEDIR);
84 textdomain(PACKAGE);
85 bind_textdomain_codeset(PACKAGE, "UTF-8");
86
87 QApplication a( argc, argv );
88
89 QCoreApplication::setOrganizationName( "uim" );
90 QCoreApplication::setApplicationName( "uim" );
91
92 KUimCharDict::Mode m = KUimCharDict::UNKNOWN;
93 for ( int i = 0; i < argc; i++ )
94 {
95 QString arg( argv[ i ] );
96 if ( !arg.isEmpty() && arg.startsWith( QLatin1String( "-mode=" ) ) )
97 {
98 QStringList list = arg.split( '=', QString::SkipEmptyParts );
99 if ( list.count() < 2 || list[ 1 ].isEmpty() )
100 continue;
101 QString mode = list[ 1 ];
102
103 if ( mode == "BUSHU" )
104 m = KUimCharDict::BUSHU;
105 else if ( mode == "UNICODE" )
106 m = KUimCharDict::UNICODE;
107 }
108 }
109
110 KUimCharDict cdict;
111 cdict.changeMode( m );
112 cdict.setWindowIcon( QIcon( UIM_PIXMAPSDIR "/uim-icon.png" ) );
113 cdict.resize( 600, 400 );
114 cdict.show();
115
116 return a.exec();
117 }
118
KUimCharDict(QWidget * parent)119 KUimCharDict::KUimCharDict( QWidget *parent )
120 : QWidget( parent )
121 {
122 #ifdef Q_WS_X11
123 // Don't give input focus to this window.
124 XWMHints *wmhints = XGetWMHints( QX11Info::display(), winId() );
125 if ( !wmhints )
126 wmhints = XAllocWMHints();
127 wmhints->flags = InputHint;
128 wmhints->input = False;
129 XSetWMHints( QX11Info::display(), winId(), wmhints );
130 XFree( wmhints );
131 #endif
132
133 setupWidgets();
134
135 readConfig();
136
137 uim_fd = uim_helper_init_client_fd( 0 );
138 }
139
~KUimCharDict()140 KUimCharDict::~KUimCharDict()
141 {
142 writeConfig();
143
144 uim_helper_close_client_fd( uim_fd );
145 }
146
setupWidgets()147 void KUimCharDict::setupWidgets()
148 {
149 QVBoxLayout * layout = new QVBoxLayout( this );
150 layout->setMargin( 4 );
151 layout->setSpacing( 6 );
152
153 QWidget *upperHWidget = new QWidget( this );
154 upperHWidget->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Maximum );
155
156 QLabel *modeLabel = new QLabel( _( "Mode:" ), upperHWidget );
157 m_modeCombo = new QComboBox( upperHWidget );
158 m_modeCombo->addItem( _( "Bushu Search" ) );
159 m_modeCombo->addItem( _( "Unicode Search" ) );
160 modeLabel->setBuddy( m_modeCombo );
161
162 m_fontselButton = new QPushButton( upperHWidget );
163 m_fontselButton->setText( _( "Select Font" ) );
164 connect( m_fontselButton, SIGNAL( clicked() ),
165 this, SLOT( slotSelectFont() ) );
166
167 QLabel *charLabel = new QLabel( _( "Chars:" ), upperHWidget );
168 m_charLineEdit = new QLineEdit( upperHWidget );
169 charLabel->setBuddy( m_charLineEdit );
170
171 QPushButton *clearButton = new QPushButton( _( "Clear" ), upperHWidget );
172 connect( clearButton, SIGNAL( clicked() ),
173 m_charLineEdit, SLOT( clear() ) );
174
175 QHBoxLayout *upperHLayout = new QHBoxLayout( upperHWidget );
176 upperHLayout->setSpacing( 4 );
177 upperHLayout->addWidget( modeLabel );
178 upperHLayout->addWidget( m_modeCombo );
179 upperHLayout->addWidget( m_fontselButton );
180 upperHLayout->addSpacing( 11 );
181 upperHLayout->addWidget( charLabel );
182 upperHLayout->addWidget( m_charLineEdit );
183 upperHLayout->addWidget( clearButton );
184
185 m_widgetStack = new QStackedWidget( this );
186 m_widgetStack->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
187
188 m_bushuView = new BushuViewWidget( this );
189 connect( m_bushuView, SIGNAL( charSelected( const QString & ) ),
190 this, SLOT( slotCharSelected( const QString & ) ) );
191 m_bushuView->hide();
192 m_widgetStack->addWidget( m_bushuView );
193
194 m_unicodeView = new UnicodeViewWidget( this );
195 connect( m_unicodeView, SIGNAL( charSelected( const QString & ) ),
196 this, SLOT( slotCharSelected( const QString & ) ) );
197 m_unicodeView->hide();
198 m_widgetStack->addWidget( m_unicodeView );
199
200 connect( m_modeCombo, SIGNAL( activated( int ) ),
201 m_widgetStack, SLOT( setCurrentIndex( int ) ) );
202
203 QFrame *separator = new QFrame( this );
204 separator->setFrameShape( QFrame::HLine );
205 separator->setFrameShadow( QFrame::Sunken );
206 layout->setMargin( 0 );
207 layout->addWidget( upperHWidget );
208 layout->addWidget( separator );
209 layout->addWidget( m_widgetStack );
210 }
211
writeConfig()212 void KUimCharDict::writeConfig()
213 {
214 QSettings settings;
215
216 // font
217 settings.setValue( "/uim-kdehelper/chardict/font", m_fontselButton->font().toString() );
218 }
readConfig()219 void KUimCharDict::readConfig()
220 {
221 QSettings settings;
222 QString str;
223
224 // font
225 str = settings.value( "/uim-kdehelper/chardict/font" ).toString();
226 if ( !str.isEmpty() )
227 {
228 QFont font;
229 font.fromString( str );
230
231 setCharDictFont( font );
232 } else {
233 setCharDictFont( font() );
234 }
235 }
236
setCharDictFont(const QFont & font)237 void KUimCharDict::setCharDictFont( const QFont &font )
238 {
239 // button
240 m_fontselButton->setFont( font );
241 // bushu
242 m_bushuView->setCharFont( font );
243 // unicode
244 m_unicodeView->setCharFont( font );
245 }
246
247
changeMode(int mode)248 void KUimCharDict::changeMode( int mode )
249 {
250 if ( mode < 0 || mode > m_widgetStack->count() - 1 )
251 {
252 qDebug( "Unknown Mode" );
253 return;
254 }
255 m_widgetStack->setCurrentIndex( mode );
256 m_modeCombo->setCurrentIndex( mode );
257 }
258
slotSelectFont()259 void KUimCharDict::slotSelectFont()
260 {
261 bool ok;
262 QFont font = QFontDialog::getFont( &ok, m_fontselButton->font(), this );
263 if ( ok )
264 {
265 // font is set to the font the user selected
266 setCharDictFont( font );
267 }
268 }
269
slotCharSelected(const QString & c)270 void KUimCharDict::slotCharSelected( const QString &c )
271 {
272 m_charLineEdit->setText( m_charLineEdit->text() + c );
273 uim_helper_send_message( uim_fd,
274 ( "commit_string\n" + c + '\n' ).toUtf8().data() );
275 }
276