1 | /* |
2 | * Copyright 1999 Reginald Stadlbauer <reggie@kde.org> |
3 | * |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License as |
6 | * published by the Free Software Foundation; either version 2 of |
7 | * the License, or (at your option) any later version. |
8 | * |
9 | * This program 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 |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | */ |
17 | |
18 | #include "kcharselectdia.h" |
19 | |
20 | #include <KAction> |
21 | #include <KActionCollection> |
22 | #include <KApplication> |
23 | #include <KConfig> |
24 | #include <KDebug> |
25 | #include <KDialog> |
26 | #include <KGlobal> |
27 | #include <KIcon> |
28 | #include <KLocale> |
29 | #include <KStandardAction> |
30 | #include <KStandardShortcut> |
31 | #include <KToggleAction> |
32 | |
33 | /******************************************************************/ |
34 | /* class KCharSelectDia */ |
35 | /******************************************************************/ |
36 | |
37 | //================================================================== |
38 | KCharSelectDia::KCharSelectDia() |
39 | : KXmlGuiWindow() |
40 | { |
41 | KSharedConfig::Ptr config = KGlobal::config(); |
42 | KConfigGroup gr = config->group("General" ); |
43 | |
44 | vFont = gr.readEntry("selectedFont" , KGlobalSettings::generalFont()); |
45 | vChr = QChar(static_cast<unsigned short>(gr.readEntry("char" , 33))); |
46 | _rtl = gr.readEntry("rtl" , false); |
47 | |
48 | QWidget *mainWidget = new QWidget(this); |
49 | setCentralWidget(mainWidget); |
50 | |
51 | grid = new QGridLayout( mainWidget ); |
52 | grid->setMargin( KDialog::marginHint() ); |
53 | grid->setSpacing( KDialog::spacingHint() ); |
54 | |
55 | // Add character selection widget from library kdeui |
56 | charSelect = new KCharSelect(mainWidget, actionCollection()); |
57 | charSelect->setCurrentChar(vChr); |
58 | charSelect->setCurrentFont(vFont); |
59 | charSelect->resize(charSelect->sizeHint()); |
60 | connect(charSelect,SIGNAL(currentCharChanged(const QChar &)), |
61 | SLOT(charChanged(const QChar &))); |
62 | connect(charSelect,SIGNAL(charSelected(const QChar &)), |
63 | SLOT(add(const QChar &))); |
64 | connect(charSelect,SIGNAL(currentFontChanged(const QFont &)), |
65 | SLOT(fontSelected(const QFont &))); |
66 | grid->addWidget(charSelect, 0, 0, 1, 4); |
67 | |
68 | // Build line editor |
69 | lined = new KLineEdit(mainWidget); |
70 | lined->resize(lined->sizeHint()); |
71 | lined->setClearButtonShown(true); |
72 | |
73 | lined->setFont( vFont ); |
74 | |
75 | connect(lined,SIGNAL(textChanged(const QString &)), |
76 | SLOT(lineEditChanged())); |
77 | grid->addWidget(lined, 1, 0, 1, 3); |
78 | |
79 | bClip = new KPushButton( KGuiItem( i18n( "&To Clipboard" ), |
80 | QLatin1String( "edit-copy" ) ), mainWidget ); |
81 | bClip->setFixedSize( bClip->sizeHint() ); |
82 | connect(bClip,SIGNAL(clicked()),this,SLOT(toClip())); |
83 | grid->addWidget(bClip, 1, 3); |
84 | |
85 | // Build menu |
86 | KStandardAction::quit( this, SLOT(close()), actionCollection() ); |
87 | |
88 | KAction *action = actionCollection()->addAction( QLatin1String( "copy_clip" ) ); |
89 | action->setText( i18n("&To Clipboard" ) ); |
90 | action->setIcon( KIcon( QLatin1String( "edit-copy" )) ); |
91 | connect(action, SIGNAL(triggered(bool)), SLOT(toClip())); |
92 | action->setShortcuts(KStandardShortcut::shortcut(KStandardShortcut::Copy)); |
93 | |
94 | action = actionCollection()->addAction( QLatin1String( "copy_utf_8" ) ); |
95 | action->setText( i18n("To Clipboard &UTF-8" ) ); |
96 | connect(action, SIGNAL(triggered(bool) ), SLOT(toClipUTF8())); |
97 | action = actionCollection()->addAction( QLatin1String( "copy_html" ) ); |
98 | action->setText( i18n("To Clipboard &HTML" ) ); |
99 | connect(action, SIGNAL(triggered(bool) ), SLOT(toClipHTML())); |
100 | |
101 | action = actionCollection()->addAction( QLatin1String( "from_clip" ) ); |
102 | action->setText( i18n("&From Clipboard" ) ); |
103 | action->setIcon( KIcon( QLatin1String( "edit-paste" )) ); |
104 | connect(action, SIGNAL(triggered(bool)), SLOT(fromClip())); |
105 | action->setShortcuts(KStandardShortcut::shortcut(KStandardShortcut::Paste)); |
106 | action = actionCollection()->addAction( QLatin1String( "from_clip_utf8" ) ); |
107 | action->setText( i18n( "From Clipboard UTF-8" ) ); |
108 | connect(action, SIGNAL(triggered(bool) ), SLOT(fromClipUTF8())); |
109 | |
110 | i18n("From Clipboard HTML" ); // Intended for future use |
111 | |
112 | action = actionCollection()->addAction( QLatin1String( "flip" ) ); |
113 | action->setText( i18n("&Flip Text" ) ); |
114 | connect(action, SIGNAL(triggered(bool) ), SLOT(flipText())); |
115 | |
116 | action = new KToggleAction( i18n("&Reverse Direction" ), this ); |
117 | action->setChecked(_rtl); |
118 | actionCollection()->addAction( QLatin1String( "rtl" ), action ); |
119 | connect(action, SIGNAL(toggled(bool) ), SLOT(setRtl(bool))); |
120 | |
121 | charSelect->setFocus(); |
122 | |
123 | if(_rtl) |
124 | lined->setAlignment( Qt::AlignRight ); |
125 | else |
126 | lined->setAlignment( Qt::AlignLeft ); |
127 | |
128 | setupGUI(ToolBar|Keys|Save|Create); |
129 | } |
130 | |
131 | //================================================================== |
132 | void KCharSelectDia::closeEvent(QCloseEvent *event) |
133 | { |
134 | KSharedConfig::Ptr config = KGlobal::config(); |
135 | KConfigGroup gr = config->group("General" ); |
136 | |
137 | gr.writeEntry("selectedFont" , vFont); |
138 | gr.writeEntry("char" , static_cast<int>(vChr.unicode())); |
139 | gr.writeEntry("rtl" , _rtl); |
140 | |
141 | KXmlGuiWindow::closeEvent(event); |
142 | } |
143 | |
144 | //================================================================== |
145 | void KCharSelectDia::charChanged(const QChar &_chr) |
146 | { |
147 | vChr = _chr; |
148 | } |
149 | |
150 | //================================================================== |
151 | void KCharSelectDia::fontSelected(const QFont &_font) |
152 | { |
153 | lined->setFont( _font ); |
154 | |
155 | vFont = _font; |
156 | } |
157 | |
158 | //================================================================== |
159 | void KCharSelectDia::add(const QChar &_chr) |
160 | { |
161 | charChanged(_chr); |
162 | |
163 | QString str = lined->text(); |
164 | const int pos = lined->cursorPosition(); |
165 | str.insert(pos, _chr); |
166 | lined->setText(str); |
167 | lined->setCursorPosition(pos + 1); |
168 | } |
169 | |
170 | //================================================================== |
171 | void KCharSelectDia::toClip() |
172 | { |
173 | QString str = lined->text(); |
174 | if (str.isEmpty()) |
175 | str = vChr; |
176 | QClipboard *cb = QApplication::clipboard(); |
177 | cb->setText(str,QClipboard::Clipboard); |
178 | cb->setText(str,QClipboard::Selection); |
179 | } |
180 | |
181 | //================================================================== |
182 | // UTF-8 is rapidly becoming the favored 8-bit encoding for |
183 | // Unicode (iso10646-1). |
184 | // |
185 | void KCharSelectDia::toClipUTF8() |
186 | { |
187 | QClipboard *cb = QApplication::clipboard(); |
188 | QString str = lined->text(); |
189 | if (str.isEmpty()) |
190 | str = vChr; |
191 | cb->setText(QLatin1String( str.toUtf8() )); |
192 | } |
193 | |
194 | //================================================================== |
195 | // Put valid HTML 4.0 into the clipboard. Valid ISO-8859-1 Latin 1 |
196 | // characters are left undisturbed. Everything else, including the |
197 | // "c0 control characters" often used by Windows, are clipped |
198 | // as a HTML entity. |
199 | // |
200 | void KCharSelectDia::toClipHTML() |
201 | { |
202 | QClipboard *cb = QApplication::clipboard(); |
203 | QString input; |
204 | QString html; |
205 | QString tempstring; |
206 | QChar tempchar; |
207 | int i = 0; |
208 | |
209 | input = lined->text(); |
210 | if (input.isEmpty()) |
211 | input = vChr; |
212 | const int inputLength = input.length(); |
213 | for(i=0; i< inputLength; ++i ) |
214 | { |
215 | tempchar = input.at(i); |
216 | if( tempchar.toLatin1() && ((tempchar.unicode() < 128) || (tempchar.unicode() >= 128+32)) ) |
217 | { |
218 | html.append(input.at(i)); |
219 | } |
220 | else |
221 | { |
222 | html.append(tempstring.sprintf("&#x%x;" , tempchar.unicode())); |
223 | } |
224 | } |
225 | cb->setText(html); |
226 | } |
227 | |
228 | //================================================================== |
229 | // |
230 | void KCharSelectDia::fromClip() |
231 | { |
232 | QClipboard *cb = QApplication::clipboard(); |
233 | lined->setText( cb->text() ); |
234 | } |
235 | |
236 | //================================================================== |
237 | // UTF-8 is rapidly becoming the favored 8-bit encoding for |
238 | // Unicode (iso10646-1). This function is handy for decoding |
239 | // UTF-8 found in legacy applications, consoles, filenames, webpages, |
240 | // etc. |
241 | // |
242 | void KCharSelectDia::fromClipUTF8() |
243 | { |
244 | QClipboard *cb = QApplication::clipboard(); |
245 | const QString str = cb->text(); |
246 | |
247 | lined->setText( str.fromUtf8( str.toLatin1() ) ); |
248 | } |
249 | |
250 | //================================================================== |
251 | // Reverse the text held in the line edit buffer. This is crucial |
252 | // for dealing with visual vs. logical representations of |
253 | // right to left languages, and handy for working around all |
254 | // manner of legacy character order issues. |
255 | // |
256 | void KCharSelectDia::flipText() |
257 | { |
258 | QString input; |
259 | QString output; |
260 | int i; |
261 | |
262 | input = lined->text(); |
263 | const int nbLength = input.length(); |
264 | for(i=0; i< nbLength; ++i ) |
265 | { |
266 | output.prepend( input.at(i) ); |
267 | } |
268 | lined->setText(output); |
269 | } |
270 | |
271 | //================================================================== |
272 | void KCharSelectDia::setRtl(bool rtl) |
273 | { |
274 | _rtl = rtl; |
275 | if(_rtl) |
276 | lined->setAlignment( Qt::AlignRight ); |
277 | else |
278 | lined->setAlignment( Qt::AlignLeft ); |
279 | } |
280 | |
281 | //================================================================== |
282 | void KCharSelectDia::lineEditChanged() |
283 | { |
284 | if(_rtl) |
285 | { |
286 | if(lined->cursorPosition()) |
287 | lined->setCursorPosition( lined->cursorPosition() - 1 ); |
288 | } |
289 | } |
290 | |
291 | #include "kcharselectdia.moc" |
292 | |