[190] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
| 2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/qwerty/qwerty.doc:4 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>Simple editor that can load encodings</title>
|
---|
| 7 | <style type="text/css"><!--
|
---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
| 9 | a:link { color: #004faf; text-decoration: none }
|
---|
| 10 | a:visited { color: #672967; text-decoration: none }
|
---|
| 11 | body { background: #ffffff; color: black; }
|
---|
| 12 | --></style>
|
---|
| 13 | </head>
|
---|
| 14 | <body>
|
---|
| 15 |
|
---|
| 16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
| 17 | <tr bgcolor="#E5E5E5">
|
---|
| 18 | <td valign=center>
|
---|
| 19 | <a href="index.html">
|
---|
| 20 | <font color="#004faf">Home</font></a>
|
---|
| 21 | | <a href="classes.html">
|
---|
| 22 | <font color="#004faf">All Classes</font></a>
|
---|
| 23 | | <a href="mainclasses.html">
|
---|
| 24 | <font color="#004faf">Main Classes</font></a>
|
---|
| 25 | | <a href="annotated.html">
|
---|
| 26 | <font color="#004faf">Annotated</font></a>
|
---|
| 27 | | <a href="groups.html">
|
---|
| 28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
| 29 | | <a href="functions.html">
|
---|
| 30 | <font color="#004faf">Functions</font></a>
|
---|
| 31 | </td>
|
---|
| 32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Simple editor that can load encodings</h1>
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | <p>
|
---|
| 36 | <hr>
|
---|
| 37 | <p> Header file:
|
---|
| 38 | <p> <pre>/****************************************************************************
|
---|
| 39 | ** $Id: qwerty-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
| 40 | **
|
---|
| 41 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
| 42 | **
|
---|
| 43 | ** This file is part of an example program for Qt. This example
|
---|
| 44 | ** program may be used, distributed and modified without limitation.
|
---|
| 45 | **
|
---|
| 46 | *****************************************************************************/
|
---|
| 47 |
|
---|
| 48 | #ifndef QWERTY_H
|
---|
| 49 | #define QWERTY_H
|
---|
| 50 |
|
---|
| 51 | #include <<a href="qwidget-h.html">qwidget.h</a>>
|
---|
| 52 | #include <<a href="qmenubar-h.html">qmenubar.h</a>>
|
---|
| 53 | #include <<a href="qmultilineedit-h.html">qmultilineedit.h</a>>
|
---|
| 54 | #include <<a href="qprinter-h.html">qprinter.h</a>>
|
---|
| 55 |
|
---|
| 56 | class Editor : public <a href="qwidget.html">QWidget</a>
|
---|
| 57 | {
|
---|
| 58 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
| 59 | public:
|
---|
| 60 | Editor( <a href="qwidget.html">QWidget</a> *parent=0, const char *name="qwerty" );
|
---|
| 61 | ~Editor();
|
---|
| 62 |
|
---|
| 63 | void load( const <a href="qstring.html">QString</a>& fileName, int code=-1 );
|
---|
| 64 |
|
---|
| 65 | public slots:
|
---|
| 66 | void newDoc();
|
---|
| 67 | void load();
|
---|
| 68 | bool save();
|
---|
| 69 | void print();
|
---|
| 70 | void addEncoding();
|
---|
| 71 | void toUpper();
|
---|
| 72 | void toLower();
|
---|
| 73 | void font();
|
---|
| 74 | protected:
|
---|
| 75 | void resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> * );
|
---|
| 76 | void closeEvent( <a href="qcloseevent.html">QCloseEvent</a> * );
|
---|
| 77 |
|
---|
| 78 | private slots:
|
---|
| 79 | void saveAsEncoding( int );
|
---|
| 80 | void openAsEncoding( int );
|
---|
| 81 | void textChanged();
|
---|
| 82 |
|
---|
| 83 | private:
|
---|
| 84 | bool saveAs( const <a href="qstring.html">QString</a>& fileName, int code=-1 );
|
---|
| 85 | void rebuildCodecList();
|
---|
| 86 | <a href="qmenubar.html">QMenuBar</a> *m;
|
---|
| 87 | <a href="qmultilineedit.html">QMultiLineEdit</a> *e;
|
---|
| 88 | #ifndef QT_NO_PRINTER
|
---|
| 89 | <a href="qprinter.html">QPrinter</a> printer;
|
---|
| 90 | #endif
|
---|
| 91 | <a href="qpopupmenu.html">QPopupMenu</a> *save_as;
|
---|
| 92 | <a href="qpopupmenu.html">QPopupMenu</a> *open_as;
|
---|
| 93 | bool changed;
|
---|
| 94 | };
|
---|
| 95 |
|
---|
| 96 | #endif // QWERTY_H
|
---|
| 97 | </pre>
|
---|
| 98 |
|
---|
| 99 | <p> <hr>
|
---|
| 100 | <p> Implementation:
|
---|
| 101 | <p> <pre>/****************************************************************************
|
---|
| 102 | ** $Id: qwerty-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
| 103 | **
|
---|
| 104 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
| 105 | **
|
---|
| 106 | ** This file is part of an example program for Qt. This example
|
---|
| 107 | ** program may be used, distributed and modified without limitation.
|
---|
| 108 | **
|
---|
| 109 | *****************************************************************************/
|
---|
| 110 |
|
---|
| 111 | #include "qwerty.h"
|
---|
| 112 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
| 113 | #include <<a href="qfile-h.html">qfile.h</a>>
|
---|
| 114 | #include <<a href="qfiledialog-h.html">qfiledialog.h</a>>
|
---|
| 115 | #include <<a href="qpopupmenu-h.html">qpopupmenu.h</a>>
|
---|
| 116 | #include <<a href="qtextstream-h.html">qtextstream.h</a>>
|
---|
| 117 | #include <<a href="qpainter-h.html">qpainter.h</a>>
|
---|
| 118 | #include <<a href="qmessagebox-h.html">qmessagebox.h</a>>
|
---|
| 119 | #include <<a href="qpaintdevicemetrics-h.html">qpaintdevicemetrics.h</a>>
|
---|
| 120 | #include <<a href="qptrlist-h.html">qptrlist.h</a>>
|
---|
| 121 | #include <<a href="qfontdialog-h.html">qfontdialog.h</a>>
|
---|
| 122 |
|
---|
| 123 | #include <<a href="qtextcodec-h.html">qtextcodec.h</a>>
|
---|
| 124 |
|
---|
| 125 | const bool no_writing = FALSE;
|
---|
| 126 |
|
---|
| 127 | static QPtrList<QTextCodec> *codecList = 0;
|
---|
| 128 |
|
---|
| 129 | enum { Uni = 0, MBug = 1, Lat1 = 2, Local = 3, Guess = 4, Codec = 5 };
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 | <a name="f235"></a>Editor::Editor( <a href="qwidget.html">QWidget</a> * parent , const char * name )
|
---|
| 133 | : <a href="qwidget.html">QWidget</a>( parent, name, WDestructiveClose )
|
---|
| 134 | {
|
---|
| 135 | m = new <a href="qmenubar.html">QMenuBar</a>( this, "menu" );
|
---|
| 136 |
|
---|
| 137 | <a href="qpopupmenu.html">QPopupMenu</a> * file = new <a href="qpopupmenu.html">QPopupMenu</a>();
|
---|
| 138 | <a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( file );
|
---|
| 139 | <a name="x371"></a> m-><a href="qmenudata.html#insertItem">insertItem</a>( "&File", file );
|
---|
| 140 |
|
---|
| 141 | file-><a href="qmenudata.html#insertItem">insertItem</a>( "&New", this, SLOT(newDoc()), ALT+Key_N );
|
---|
| 142 | file-><a href="qmenudata.html#insertItem">insertItem</a>( "&Open...", this, SLOT(load()), ALT+Key_O );
|
---|
| 143 | file-><a href="qmenudata.html#insertItem">insertItem</a>( "&Save...", this, SLOT(save()), ALT+Key_S );
|
---|
| 144 | <a name="x372"></a> file-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
---|
| 145 | open_as = new <a href="qpopupmenu.html">QPopupMenu</a>();
|
---|
| 146 | file-><a href="qmenudata.html#insertItem">insertItem</a>( "Open &As", open_as );
|
---|
| 147 | save_as = new <a href="qpopupmenu.html">QPopupMenu</a>();
|
---|
| 148 | file-><a href="qmenudata.html#insertItem">insertItem</a>( "Sa&ve As", save_as );
|
---|
| 149 | file-><a href="qmenudata.html#insertItem">insertItem</a>( "Add &Encoding", this, SLOT(addEncoding()) );
|
---|
| 150 | #ifndef QT_NO_PRINTER
|
---|
| 151 | file-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
---|
| 152 | file-><a href="qmenudata.html#insertItem">insertItem</a>( "&Print...", this, SLOT(print()), ALT+Key_P );
|
---|
| 153 | #endif
|
---|
| 154 | file-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
---|
| 155 | file-><a href="qmenudata.html#insertItem">insertItem</a>( "&Close", this, SLOT(<a href="qwidget.html#close">close</a>()),ALT+Key_W );
|
---|
| 156 | <a name="x358"></a> file-><a href="qmenudata.html#insertItem">insertItem</a>( "&Quit", qApp, SLOT(<a href="qapplication.html#closeAllWindows">closeAllWindows</a>()), ALT+Key_Q );
|
---|
| 157 |
|
---|
| 158 | <a name="x386"></a> <a href="qobject.html#connect">connect</a>( save_as, SIGNAL(<a href="qpopupmenu.html#activated">activated</a>(int)), this, SLOT(saveAsEncoding(int)) );
|
---|
| 159 | <a href="qobject.html#connect">connect</a>( open_as, SIGNAL(<a href="qpopupmenu.html#activated">activated</a>(int)), this, SLOT(openAsEncoding(int)) );
|
---|
| 160 | rebuildCodecList();
|
---|
| 161 |
|
---|
| 162 | <a href="qpopupmenu.html">QPopupMenu</a> * edit = new <a href="qpopupmenu.html">QPopupMenu</a>();
|
---|
| 163 | <a href="qapplication.html#Q_CHECK_PTR">Q_CHECK_PTR</a>( edit );
|
---|
| 164 | m-><a href="qmenudata.html#insertItem">insertItem</a>( "&Edit", edit );
|
---|
| 165 |
|
---|
| 166 | edit-><a href="qmenudata.html#insertItem">insertItem</a>( "To &Uppercase", this, SLOT(toUpper()), ALT+Key_U );
|
---|
| 167 | edit-><a href="qmenudata.html#insertItem">insertItem</a>( "To &Lowercase", this, SLOT(toLower()), ALT+Key_L );
|
---|
| 168 | #ifndef QT_NO_FONTDIALOG
|
---|
| 169 | edit-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
---|
| 170 | edit-><a href="qmenudata.html#insertItem">insertItem</a>( "&Select Font" , this, SLOT(<a href="qwidget.html#font">font</a>()), ALT+Key_T );
|
---|
| 171 | #endif
|
---|
| 172 | changed = FALSE;
|
---|
| 173 | e = new <a href="qmultilineedit.html">QMultiLineEdit</a>( this, "editor" );
|
---|
| 174 | <a name="x400"></a> <a href="qobject.html#connect">connect</a>( e, SIGNAL( <a href="qtextedit.html#textChanged">textChanged</a>() ), this, SLOT( textChanged() ) );
|
---|
| 175 |
|
---|
| 176 | // We use Unifont - if you have it installed you'll see all
|
---|
| 177 | // Unicode character glyphs.
|
---|
| 178 | //
|
---|
| 179 | // Unifont only comes in one pixel size, so we cannot let
|
---|
| 180 | // it change pixel size as the display DPI changes.
|
---|
| 181 | //
|
---|
| 182 | <a name="x367"></a> <a href="qfont.html">QFont</a> unifont("unifont",16,50); unifont.<a href="qfont.html#setPixelSize">setPixelSize</a>(16);
|
---|
| 183 | <a name="x397"></a> e-><a href="qwidget.html#setFont">setFont</a>( unifont );
|
---|
| 184 |
|
---|
| 185 | e-><a href="qwidget.html#setFocus">setFocus</a>();
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | Editor::~Editor()
|
---|
| 189 | {
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | <a name="x405"></a>void Editor::<a href="qwidget.html#font">font</a>()
|
---|
| 193 | {
|
---|
| 194 | #ifndef QT_NO_FONTDIALOG
|
---|
| 195 | bool ok;
|
---|
| 196 | <a name="x396"></a><a name="x368"></a> <a href="qfont.html">QFont</a> f = QFontDialog::<a href="qfontdialog.html#getFont">getFont</a>( &ok, e-><a href="qtextedit.html#font">font</a>() );
|
---|
| 197 | if ( ok ) {
|
---|
| 198 | e-><a href="qwidget.html#setFont">setFont</a>( f );
|
---|
| 199 | }
|
---|
| 200 | #endif
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 |
|
---|
| 204 |
|
---|
| 205 | void <a name="f236"></a>Editor::rebuildCodecList()
|
---|
| 206 | {
|
---|
| 207 | delete codecList;
|
---|
| 208 | codecList = new <a href="qptrlist.html">QPtrList</a><QTextCodec>;
|
---|
| 209 | <a href="qtextcodec.html">QTextCodec</a> *codec;
|
---|
| 210 | int i;
|
---|
| 211 | <a name="x392"></a> for (i = 0; (codec = QTextCodec::<a href="qtextcodec.html#codecForIndex">codecForIndex</a>(i)); i++)
|
---|
| 212 | <a name="x387"></a> codecList-><a href="qptrlist.html#append">append</a>( codec );
|
---|
| 213 | <a name="x389"></a> int n = codecList-><a href="qptrlist.html#count">count</a>();
|
---|
| 214 | for (int pm=0; pm<2; pm++) {
|
---|
| 215 | <a href="qpopupmenu.html">QPopupMenu</a>* menu = pm ? open_as : save_as;
|
---|
| 216 | <a name="x370"></a> menu-><a href="qmenudata.html#clear">clear</a>();
|
---|
| 217 | <a href="qstring.html">QString</a> local = "Local (";
|
---|
| 218 | <a name="x393"></a> local += QTextCodec::<a href="qtextcodec.html#codecForLocale">codecForLocale</a>()->name();
|
---|
| 219 | local += ")";
|
---|
| 220 | menu-><a href="qmenudata.html#insertItem">insertItem</a>( local, Local );
|
---|
| 221 | menu-><a href="qmenudata.html#insertItem">insertItem</a>( "Unicode", Uni );
|
---|
| 222 | menu-><a href="qmenudata.html#insertItem">insertItem</a>( "Latin1", Lat1 );
|
---|
| 223 | menu-><a href="qmenudata.html#insertItem">insertItem</a>( "Microsoft Unicode", MBug );
|
---|
| 224 | if ( pm )
|
---|
| 225 | menu-><a href="qmenudata.html#insertItem">insertItem</a>( "[guess]", Guess );
|
---|
| 226 | for ( i = 0; i < n; i++ )
|
---|
| 227 | <a name="x388"></a> menu-><a href="qmenudata.html#insertItem">insertItem</a>( codecList-><a href="qptrlist.html#at">at</a>(i)->name(), Codec + i );
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | void <a name="f237"></a>Editor::newDoc()
|
---|
| 232 | {
|
---|
| 233 | Editor *ed = new Editor;
|
---|
| 234 | if ( qApp-><a href="qapplication.html#desktop">desktop</a>()->size().width() < 450
|
---|
| 235 | || qApp-><a href="qapplication.html#desktop">desktop</a>()->size().height() < 450 ) {
|
---|
| 236 | <a name="x413"></a> ed-><a href="qwidget.html#showMaximized">showMaximized</a>();
|
---|
| 237 | } else {
|
---|
| 238 | ed-><a href="qwidget.html#resize">resize</a>( 400, 400 );
|
---|
| 239 | ed-><a href="qwidget.html#show">show</a>();
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 |
|
---|
| 244 | void <a name="f238"></a>Editor::load()
|
---|
| 245 | {
|
---|
| 246 | #ifndef QT_NO_FILEDIALOG
|
---|
| 247 | <a name="x365"></a> <a href="qstring.html">QString</a> fn = QFileDialog::<a href="qfiledialog.html#getOpenFileName">getOpenFileName</a>( <a href="qstring.html#QString-null">QString::null</a>, QString::null, this );
|
---|
| 248 | <a name="x390"></a> if ( !fn.<a href="qstring.html#isEmpty">isEmpty</a>() )
|
---|
| 249 | load( fn, -1 );
|
---|
| 250 | #endif
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | void Editor::load( const <a href="qstring.html">QString</a>& fileName, int code )
|
---|
| 254 | {
|
---|
| 255 | <a href="qfile.html">QFile</a> f( fileName );
|
---|
| 256 | <a name="x363"></a> if ( !f.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_ReadOnly</a> ) )
|
---|
| 257 | return;
|
---|
| 258 |
|
---|
| 259 | <a name="x376"></a> e-><a href="qmultilineedit.html#setAutoUpdate">setAutoUpdate</a>( FALSE );
|
---|
| 260 |
|
---|
| 261 | <a href="qtextstream.html">QTextStream</a> t(&f);
|
---|
| 262 | if ( code >= Codec )
|
---|
| 263 | <a name="x402"></a> t.<a href="qtextstream.html#setCodec">setCodec</a>( codecList-><a href="qptrlist.html#at">at</a>(code-Codec) );
|
---|
| 264 | else if ( code == Uni )
|
---|
| 265 | <a name="x403"></a> t.<a href="qtextstream.html#setEncoding">setEncoding</a>( QTextStream::Unicode );
|
---|
| 266 | else if ( code == MBug )
|
---|
| 267 | t.<a href="qtextstream.html#setEncoding">setEncoding</a>( QTextStream::UnicodeReverse );
|
---|
| 268 | else if ( code == Lat1 )
|
---|
| 269 | t.<a href="qtextstream.html#setEncoding">setEncoding</a>( QTextStream::Latin1 );
|
---|
| 270 | else if ( code == Guess ) {
|
---|
| 271 | <a href="qfile.html">QFile</a> f(fileName);
|
---|
| 272 | f.<a href="qfile.html#open">open</a>(IO_ReadOnly);
|
---|
| 273 | char buffer[256];
|
---|
| 274 | int l = 256;
|
---|
| 275 | <a name="x364"></a> l=f.<a href="qiodevice.html#readBlock">readBlock</a>(buffer,l);
|
---|
| 276 | <a name="x391"></a> <a href="qtextcodec.html">QTextCodec</a>* codec = QTextCodec::<a href="qtextcodec.html#codecForContent">codecForContent</a>(buffer, l);
|
---|
| 277 | if ( codec ) {
|
---|
| 278 | <a name="x395"></a><a name="x373"></a> QMessageBox::<a href="qmessagebox.html#information">information</a>(this,"Encoding",QString("Codec: ")+codec-><a href="qtextcodec.html#name">name</a>());
|
---|
| 279 | t.<a href="qtextstream.html#setCodec">setCodec</a>( codec );
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 | <a name="x401"></a><a name="x398"></a> e-><a href="qtextedit.html#setText">setText</a>( t.<a href="qtextstream.html#read">read</a>() );
|
---|
| 283 | <a name="x362"></a> f.<a href="qfile.html#close">close</a>();
|
---|
| 284 |
|
---|
| 285 | e-><a href="qmultilineedit.html#setAutoUpdate">setAutoUpdate</a>( TRUE );
|
---|
| 286 | <a name="x407"></a> e-><a href="qwidget.html#repaint">repaint</a>();
|
---|
| 287 | <a href="qwidget.html#setCaption">setCaption</a>( fileName );
|
---|
| 288 |
|
---|
| 289 | changed = FALSE;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | void <a name="f239"></a>Editor::openAsEncoding( int code )
|
---|
| 293 | {
|
---|
| 294 | #ifndef QT_NO_FILEDIALOG
|
---|
| 295 | //storing filename (proper save) is left as an exercise...
|
---|
| 296 | <a href="qstring.html">QString</a> fn = QFileDialog::<a href="qfiledialog.html#getOpenFileName">getOpenFileName</a>( QString::null, QString::null, this );
|
---|
| 297 | if ( !fn.<a href="qstring.html#isEmpty">isEmpty</a>() )
|
---|
| 298 | (void) load( fn, code );
|
---|
| 299 | #endif
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | bool <a name="f240"></a>Editor::save()
|
---|
| 303 | {
|
---|
| 304 | #ifndef QT_NO_FILEDIALOG
|
---|
| 305 | //storing filename (proper save) is left as an exercise...
|
---|
| 306 | <a name="x366"></a> <a href="qstring.html">QString</a> fn = QFileDialog::<a href="qfiledialog.html#getSaveFileName">getSaveFileName</a>( QString::null, QString::null, this );
|
---|
| 307 | if ( !fn.<a href="qstring.html#isEmpty">isEmpty</a>() )
|
---|
| 308 | return saveAs( fn );
|
---|
| 309 | return FALSE;
|
---|
| 310 | #endif
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | void <a name="f241"></a>Editor::saveAsEncoding( int code )
|
---|
| 314 | {
|
---|
| 315 | #ifndef QT_NO_FILEDIALOG
|
---|
| 316 | //storing filename (proper save) is left as an exercise...
|
---|
| 317 | <a href="qstring.html">QString</a> fn = QFileDialog::<a href="qfiledialog.html#getSaveFileName">getSaveFileName</a>( QString::null, QString::null, this );
|
---|
| 318 | if ( !fn.<a href="qstring.html#isEmpty">isEmpty</a>() )
|
---|
| 319 | (void) saveAs( fn, code );
|
---|
| 320 | #endif
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | void <a name="f242"></a>Editor::addEncoding()
|
---|
| 324 | {
|
---|
| 325 | #ifndef QT_NO_FILEDIALOG
|
---|
| 326 | <a href="qstring.html">QString</a> fn = QFileDialog::<a href="qfiledialog.html#getOpenFileName">getOpenFileName</a>( QString::null, "*.map", this );
|
---|
| 327 | if ( !fn.<a href="qstring.html#isEmpty">isEmpty</a>() ) {
|
---|
| 328 | <a href="qfile.html">QFile</a> f(fn);
|
---|
| 329 | if (f.<a href="qfile.html#open">open</a>(IO_ReadOnly)) {
|
---|
| 330 | <a name="x394"></a> if (QTextCodec::<a href="qtextcodec.html#loadCharmap">loadCharmap</a>(&f)) {
|
---|
| 331 | rebuildCodecList();
|
---|
| 332 | } else {
|
---|
| 333 | <a name="x374"></a> QMessageBox::<a href="qmessagebox.html#warning">warning</a>(0,"Charmap error",
|
---|
| 334 | "The file did not contain a valid charmap.\n\n"
|
---|
| 335 | "A charmap file should look like this:\n"
|
---|
| 336 | " <code_set_name> thename\n"
|
---|
| 337 | " <escape_char> /\n"
|
---|
| 338 | " % alias thealias\n"
|
---|
| 339 | " CHARMAP\n"
|
---|
| 340 | " <tokenname> /x12 <U3456>\n"
|
---|
| 341 | " <tokenname> /xAB/x12 <U0023>\n"
|
---|
| 342 | " ...\n"
|
---|
| 343 | " END CHARMAP\n"
|
---|
| 344 | );
|
---|
| 345 | }
|
---|
| 346 | }
|
---|
| 347 | }
|
---|
| 348 | #endif
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 |
|
---|
| 352 | bool <a name="f243"></a>Editor::saveAs( const <a href="qstring.html">QString</a>& fileName, int code )
|
---|
| 353 | {
|
---|
| 354 | <a href="qfile.html">QFile</a> f( fileName );
|
---|
| 355 | if ( no_writing || !f.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_WriteOnly</a> ) ) {
|
---|
| 356 | QMessageBox::<a href="qmessagebox.html#warning">warning</a>(this,"I/O Error",
|
---|
| 357 | QString("The file could not be opened.\n\n")
|
---|
| 358 | +fileName);
|
---|
| 359 | return FALSE;
|
---|
| 360 | }
|
---|
| 361 | <a href="qtextstream.html">QTextStream</a> t(&f);
|
---|
| 362 | if ( code >= Codec )
|
---|
| 363 | t.<a href="qtextstream.html#setCodec">setCodec</a>( codecList-><a href="qptrlist.html#at">at</a>(code-Codec) );
|
---|
| 364 | else if ( code == Uni )
|
---|
| 365 | t.<a href="qtextstream.html#setEncoding">setEncoding</a>( QTextStream::Unicode );
|
---|
| 366 | else if ( code == MBug )
|
---|
| 367 | t.<a href="qtextstream.html#setEncoding">setEncoding</a>( QTextStream::UnicodeReverse );
|
---|
| 368 | else if ( code == Lat1 )
|
---|
| 369 | t.<a href="qtextstream.html#setEncoding">setEncoding</a>( QTextStream::Latin1 );
|
---|
| 370 | <a name="x399"></a> t << e-><a href="qtextedit.html#text">text</a>();
|
---|
| 371 | f.<a href="qfile.html#close">close</a>();
|
---|
| 372 | <a href="qwidget.html#setCaption">setCaption</a>( fileName );
|
---|
| 373 | changed = FALSE;
|
---|
| 374 | return TRUE;
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 | void <a name="f244"></a>Editor::print()
|
---|
| 378 | {
|
---|
| 379 | #ifndef QT_NO_PRINTER
|
---|
| 380 | if ( printer.setup(this) ) { // opens printer dialog
|
---|
| 381 | printer.setFullPage(TRUE); // we'll set our own margins
|
---|
| 382 | <a href="qpainter.html">QPainter</a> p;
|
---|
| 383 | p.<a href="qpainter.html#begin">begin</a>( &printer ); // paint on printer
|
---|
| 384 | <a name="x385"></a> p.<a href="qpainter.html#setFont">setFont</a>( e-><a href="qtextedit.html#font">font</a>() );
|
---|
| 385 | <a name="x384"></a> <a href="qfontmetrics.html">QFontMetrics</a> fm = p.<a href="qpainter.html#fontMetrics">fontMetrics</a>();
|
---|
| 386 | <a href="qpaintdevicemetrics.html">QPaintDeviceMetrics</a> metrics( &printer ); // need width/height
|
---|
| 387 | // of printer surface
|
---|
| 388 | <a name="x379"></a> const int MARGIN = metrics.<a href="qpaintdevicemetrics.html#logicalDpiX">logicalDpiX</a>() / 2; // half-inch margin
|
---|
| 389 | int yPos = MARGIN; // y position for each line
|
---|
| 390 |
|
---|
| 391 | <a name="x375"></a> for( int i = 0 ; i < e-><a href="qmultilineedit.html#numLines">numLines</a>() ; i++ ) {
|
---|
| 392 | if ( printer.aborted() )
|
---|
| 393 | break;
|
---|
| 394 | <a name="x378"></a><a name="x369"></a> if ( yPos + fm.<a href="qfontmetrics.html#lineSpacing">lineSpacing</a>() > metrics.<a href="qpaintdevicemetrics.html#height">height</a>() - MARGIN ) {
|
---|
| 395 | // no more room on this page
|
---|
| 396 | if ( !printer.newPage() ) // start new page
|
---|
| 397 | break; // some error
|
---|
| 398 | yPos = MARGIN; // back to top of page
|
---|
| 399 | }
|
---|
| 400 | <a name="x380"></a> p.<a href="qpainter.html#drawText">drawText</a>( MARGIN, yPos, metrics.<a href="qpaintdevicemetrics.html#width">width</a>() - 2*MARGIN,
|
---|
| 401 | <a name="x377"></a> fm.<a href="qfontmetrics.html#lineSpacing">lineSpacing</a>(), ExpandTabs, e-><a href="qmultilineedit.html#textLine">textLine</a>( i ) );
|
---|
| 402 | yPos += fm.<a href="qfontmetrics.html#lineSpacing">lineSpacing</a>();
|
---|
| 403 | }
|
---|
| 404 | p.<a href="qpainter.html#end">end</a>(); // send job to printer
|
---|
| 405 | }
|
---|
| 406 | #endif
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | void Editor::<a href="qwidget.html#resizeEvent">resizeEvent</a>( <a href="qresizeevent.html">QResizeEvent</a> * )
|
---|
| 410 | {
|
---|
| 411 | if ( e && m )
|
---|
| 412 | e-><a href="qwidget.html#setGeometry">setGeometry</a>( 0, m-><a href="qwidget.html#height">height</a>(), width(), height() - m-><a href="qwidget.html#height">height</a>() );
|
---|
| 413 | }
|
---|
| 414 |
|
---|
| 415 | <a name="x404"></a>void Editor::<a href="qwidget.html#closeEvent">closeEvent</a>( <a href="qcloseevent.html">QCloseEvent</a> *event )
|
---|
| 416 | {
|
---|
| 417 | <a name="x360"></a> event-><a href="qcloseevent.html#accept">accept</a>();
|
---|
| 418 |
|
---|
| 419 | if ( changed ) { // the text has been changed
|
---|
| 420 | switch ( QMessageBox::<a href="qmessagebox.html#warning">warning</a>( this, "Qwerty",
|
---|
| 421 | "Save changes to Document?",
|
---|
| 422 | <a href="qobject.html#tr">tr</a>("&Yes"),
|
---|
| 423 | <a href="qobject.html#tr">tr</a>("&No"),
|
---|
| 424 | <a href="qobject.html#tr">tr</a>("Cancel"),
|
---|
| 425 | 0, 2) ) {
|
---|
| 426 | case 0: // yes
|
---|
| 427 | if ( save() )
|
---|
| 428 | event-><a href="qcloseevent.html#accept">accept</a>();
|
---|
| 429 | else
|
---|
| 430 | <a name="x361"></a> event-><a href="qcloseevent.html#ignore">ignore</a>();
|
---|
| 431 | break;
|
---|
| 432 | case 1: // no
|
---|
| 433 | event-><a href="qcloseevent.html#accept">accept</a>();
|
---|
| 434 | break;
|
---|
| 435 | default: // cancel
|
---|
| 436 | event-><a href="qcloseevent.html#ignore">ignore</a>();
|
---|
| 437 | break;
|
---|
| 438 | }
|
---|
| 439 | }
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | void <a name="f245"></a>Editor::toUpper()
|
---|
| 443 | {
|
---|
| 444 | e-><a href="qtextedit.html#setText">setText</a>(e-><a href="qtextedit.html#text">text</a>().upper());
|
---|
| 445 | }
|
---|
| 446 |
|
---|
| 447 | void <a name="f246"></a>Editor::toLower()
|
---|
| 448 | {
|
---|
| 449 | e-><a href="qtextedit.html#setText">setText</a>(e-><a href="qtextedit.html#text">text</a>().lower());
|
---|
| 450 | }
|
---|
| 451 |
|
---|
| 452 | void <a name="f247"></a>Editor::textChanged()
|
---|
| 453 | {
|
---|
| 454 | changed = TRUE;
|
---|
| 455 | }
|
---|
| 456 | </pre>
|
---|
| 457 |
|
---|
| 458 | <p> <hr>
|
---|
| 459 | <p> Main:
|
---|
| 460 | <p> <pre>/****************************************************************************
|
---|
| 461 | ** $Id: qwerty-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
| 462 | **
|
---|
| 463 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
| 464 | **
|
---|
| 465 | ** This file is part of an example program for Qt. This example
|
---|
| 466 | ** program may be used, distributed and modified without limitation.
|
---|
| 467 | **
|
---|
| 468 | *****************************************************************************/
|
---|
| 469 |
|
---|
| 470 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
| 471 | #include "qwerty.h"
|
---|
| 472 |
|
---|
| 473 |
|
---|
| 474 | int main( int argc, char **argv )
|
---|
| 475 | {
|
---|
| 476 | <a href="qapplication.html">QApplication</a> a( argc, argv );
|
---|
| 477 |
|
---|
| 478 |
|
---|
| 479 | <a name="x414"></a> bool isSmall = qApp-><a href="qapplication.html#desktop">desktop</a>()->size().width() < 450
|
---|
| 480 | || qApp-><a href="qapplication.html#desktop">desktop</a>()->size().height() < 450;
|
---|
| 481 |
|
---|
| 482 | int i;
|
---|
| 483 | for ( i= argc <= 1 ? 0 : 1; i<argc; i++ ) {
|
---|
| 484 | Editor *e = new Editor;
|
---|
| 485 | e-><a href="qwidget.html#setCaption">setCaption</a>("Qt Example - QWERTY");
|
---|
| 486 | if ( i > 0 )
|
---|
| 487 | e->load( argv[i] );
|
---|
| 488 | if ( isSmall ) {
|
---|
| 489 | <a name="x422"></a> e-><a href="qwidget.html#showMaximized">showMaximized</a>();
|
---|
| 490 | } else {
|
---|
| 491 | e-><a href="qwidget.html#resize">resize</a>( 400, 400 );
|
---|
| 492 | e-><a href="qwidget.html#show">show</a>();
|
---|
| 493 | }
|
---|
| 494 | }
|
---|
| 495 | <a name="x416"></a> a.<a href="qobject.html#connect">connect</a>( &a, SIGNAL(<a href="qapplication.html#lastWindowClosed">lastWindowClosed</a>()), &a, SLOT(<a href="qapplication.html#quit">quit</a>()) );
|
---|
| 496 | return a.<a href="qapplication.html#exec">exec</a>();
|
---|
| 497 | }
|
---|
| 498 | </pre>
|
---|
| 499 |
|
---|
| 500 | <p>See also <a href="examples.html">Examples</a>.
|
---|
| 501 |
|
---|
| 502 | <!-- eof -->
|
---|
| 503 | <p><address><hr><div align=center>
|
---|
| 504 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 505 | <td>Copyright © 2007
|
---|
| 506 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 507 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 508 | </table></div></address></body>
|
---|
| 509 | </html>
|
---|