summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/applets/notesapplet/notes.cpp32
1 files changed, 22 insertions, 10 deletions
diff --git a/noncore/applets/notesapplet/notes.cpp b/noncore/applets/notesapplet/notes.cpp
index a4aa093..cc5245c 100644
--- a/noncore/applets/notesapplet/notes.cpp
+++ b/noncore/applets/notesapplet/notes.cpp
@@ -24,340 +24,352 @@
24#include <qpe/qpeapplication.h> 24#include <qpe/qpeapplication.h>
25#include <qpe/timestring.h> 25#include <qpe/timestring.h>
26#include <qpe/resource.h> 26#include <qpe/resource.h>
27#include <qpe/config.h> 27#include <qpe/config.h>
28#include <qpe/applnk.h> 28#include <qpe/applnk.h>
29#include <qpe/config.h> 29#include <qpe/config.h>
30#include <qsocket.h> 30#include <qsocket.h>
31 31
32#include <qmultilineedit.h> 32#include <qmultilineedit.h>
33#include <qlistbox.h> 33#include <qlistbox.h>
34#include <qpopupmenu.h> 34#include <qpopupmenu.h>
35#include <qmessagebox.h> 35#include <qmessagebox.h>
36 36
37#include <qdir.h> 37#include <qdir.h>
38#include <qpoint.h> 38#include <qpoint.h>
39#include <qpushbutton.h> 39#include <qpushbutton.h>
40#include <qpainter.h> 40#include <qpainter.h>
41#include <qlayout.h> 41#include <qlayout.h>
42#include <qframe.h> 42#include <qframe.h>
43#include <qpixmap.h> 43#include <qpixmap.h>
44#include <qstring.h> 44#include <qstring.h>
45#include <qtimer.h> 45#include <qtimer.h>
46 46
47/* XPM */ 47/* XPM */
48static char * notes_xpm[] = { 48static char * notes_xpm[] = {
49"16 16 11 1", 49"16 16 11 1",
50" c None", 50" c None",
51". c #000000", 51". c #000000",
52"+ c #7F7F7F", 52"+ c #7F7F7F",
53"@ c #BFBFBF", 53"@ c #BFBFBF",
54"# c #BFC1FF", 54"# c #BFC1FF",
55"$ c #FF0000", 55"$ c #FF0000",
56"% c #FFFFFF", 56"% c #FFFFFF",
57"& c #00037F", 57"& c #00037F",
58"* c #0006FF", 58"* c #0006FF",
59"= c #0005BF", 59"= c #0005BF",
60"- c #7F0000", 60"- c #7F0000",
61" .. ", 61" .. ",
62" .. ", 62" .. ",
63" ... ", 63" ... ",
64" .+ ", 64" .+ ",
65" .@. ", 65" .@. ",
66" . .+ ", 66" . .+ ",
67" ..#.@. ", 67" ..#.@. ",
68" ..###.+.. ", 68" ..###.+.. ",
69" ..###$...##.. ", 69" ..###$...##.. ",
70"..###$$$%+$$##&.", 70"..###$$$%+$$##&.",
71".*=####$-###&=&.", 71".*=####$-###&=&.",
72".=**=###==&=&=..", 72".=**=###==&=&=..",
73" ..=**=#&=&=.. ", 73" ..=**=#&=&=.. ",
74" ..=*=&=.. ", 74" ..=*=&=.. ",
75" ..=.. ", 75" ..=.. ",
76" . "}; 76" . "};
77 77
78 78
79NotesControl::NotesControl( QWidget *parent, const char *name ) 79NotesControl::NotesControl( QWidget *parent, const char *name )
80 : QFrame( parent, name,/* WDestructiveClose | */WStyle_StaysOnTop ) 80 : QFrame( parent, name,/* WDestructiveClose | */WStyle_StaysOnTop )
81// : QFrame( parent, name, WDestructiveClose | WStyle_StaysOnTop | WType_Popup ) 81// : QFrame( parent, name, WDestructiveClose | WStyle_StaysOnTop | WType_Popup )
82{ 82{
83 setFrameStyle( QFrame::PopupPanel | QFrame::Raised ); 83 setFrameStyle( QFrame::PopupPanel | QFrame::Raised );
84 loaded=false; 84 loaded=false;
85 edited=false; 85 edited=false;
86 doPopulate=true; 86 doPopulate=true;
87 QVBoxLayout *vbox = new QVBoxLayout( this,0, -1, "Vlayout" ); 87 QVBoxLayout *vbox = new QVBoxLayout( this,0, -1, "Vlayout" );
88 QHBoxLayout *hbox = new QHBoxLayout( this ); 88 QHBoxLayout *hbox = new QHBoxLayout( this, 0, -1, "HLayout" );
89 89
90 view = new QMultiLineEdit(this, "OpieNotesView"); 90 view = new QMultiLineEdit(this, "OpieNotesView");
91 91
92 box = new QListBox(this, "OpieNotesBox"); 92 box = new QListBox(this, "OpieNotesBox");
93 QPEApplication::setStylusOperation( box->viewport(),QPEApplication::RightOnHold); 93 QPEApplication::setStylusOperation( box->viewport(),QPEApplication::RightOnHold);
94 box->setFixedHeight(50); 94 box->setFixedHeight(50);
95 95
96 vbox->setMargin( 6 ); 96 vbox->setMargin( 6 );
97 vbox->setSpacing( 3 ); 97 vbox->setSpacing( 3 );
98 98
99 vbox->addWidget( view); 99 vbox->addWidget( view);
100 vbox->addWidget( box); 100 vbox->addWidget( box);
101 101
102 setFixedHeight(180); 102 setFixedHeight(180);
103 QWidget *wid = QPEApplication::desktop(); 103 QWidget *wid = QPEApplication::desktop();
104 setFixedWidth( wid->width()-10 /*200*/); 104 setFixedWidth( wid->width()-10 /*200*/);
105 105
106 setFocusPolicy(QWidget::StrongFocus); 106 setFocusPolicy(QWidget::StrongFocus);
107 107
108 newButton= new QPushButton( this, "newButton" ); 108 newButton= new QPushButton( this, "newButton" );
109 newButton->setText(tr("New")); 109 newButton->setText(tr("New"));
110 hbox->addWidget( newButton); 110 hbox->addWidget( newButton);
111
112 deleteButton= new QPushButton( this, "deleteButton" );
113 deleteButton->setText(tr("Delete"));
114 hbox->addWidget( deleteButton);
115
111 vbox->addItem(hbox); 116 vbox->addItem(hbox);
112 117
113 connect( box, SIGNAL( mouseButtonPressed( int, QListBoxItem *, const QPoint&)), 118 connect( box, SIGNAL( mouseButtonPressed( int, QListBoxItem *, const QPoint&)),
114 this,SLOT( boxPressed(int, QListBoxItem *, const QPoint&)) ); 119 this,SLOT( boxPressed(int, QListBoxItem *, const QPoint&)) );
115 connect(box, SIGNAL(highlighted(const QString&)), this, SLOT(slotBoxSelected(const QString &))); 120 connect(box, SIGNAL(highlighted(const QString&)), this, SLOT(slotBoxSelected(const QString &)));
116 connect( &menuTimer, SIGNAL( timeout() ), SLOT( showMenu() ) ); 121 connect( &menuTimer, SIGNAL( timeout() ), SLOT( showMenu() ) );
117 122
118 connect(view,SIGNAL( textChanged() ), this, SLOT(slotViewEdited() ) ); 123 connect(view,SIGNAL( textChanged() ), this, SLOT(slotViewEdited() ) );
119 connect(newButton, SIGNAL(clicked()), this, SLOT(slotNewButton())); 124 connect(newButton, SIGNAL(clicked()), this, SLOT(slotNewButton()));
125 connect(deleteButton, SIGNAL(clicked()), this, SLOT(slotDeleteButton()));
120 populateBox(); 126 populateBox();
121 load(); 127 load();
122 setCaption("Notes"); 128 setCaption("Notes");
123 // parent->setFocus(); 129 // parent->setFocus();
124} 130}
125 131
126void NotesControl::slotDeleteButton() { 132void NotesControl::slotDeleteButton() {
133
127 QString selectedText = box->currentText(); 134 QString selectedText = box->currentText();
128 qDebug("deleting "+selectedText); 135 qDebug("deleting "+selectedText);
136
137 if( !selectedText.isEmpty()) {
129 Config cfg("Notes"); 138 Config cfg("Notes");
130 cfg.setGroup("Docs"); 139 cfg.setGroup("Docs");
131 int noOfFiles = cfg.readNumEntry("NumberOfFiles", 0 ); 140 int noOfFiles = cfg.readNumEntry("NumberOfFiles", 0 );
132 QString entryName, entryName2;; 141 QString entryName, entryName2;;
133 for ( int i = 0; i < noOfFiles; i++ ) { 142 for ( int i = 0; i < noOfFiles; i++ ) {
134 entryName.sprintf( "File%i", i + 1 ); 143 entryName.sprintf( "File%i", i + 1 );
135 if(selectedText == cfg.readEntry( entryName )) { 144 if(selectedText == cfg.readEntry( entryName )) {
136 qDebug("removing %s, %d", selectedText.latin1(), i); 145 qDebug("removing %s, %d", selectedText.latin1(), i);
137 for ( int j = i; j < noOfFiles; j++ ) { 146 for ( int j = i; j < noOfFiles; j++ ) {
138 entryName.sprintf( "File%i", i + 1 ); 147 entryName.sprintf( "File%i", i + 1 );
139 entryName2.sprintf( "File%i", i + 2 ); 148 entryName2.sprintf( "File%i", i + 2 );
140 QString temp = cfg.readEntry(entryName2); 149 QString temp = cfg.readEntry(entryName2);
141 qDebug("move "+temp); 150 qDebug("move "+temp);
142 cfg.writeEntry(entryName, temp); 151 cfg.writeEntry(entryName, temp);
143 i++; 152 i++;
144 } 153 }
145 cfg.writeEntry("NumberOfFiles", noOfFiles-1 ); 154 cfg.writeEntry("NumberOfFiles", noOfFiles-1 );
146 entryName.sprintf( "File%i", noOfFiles ); 155 entryName.sprintf( "File%i", noOfFiles );
147 cfg.removeEntry(entryName); 156 cfg.removeEntry(entryName);
148 cfg.write(); 157 cfg.write();
149 DocLnk nf(selectedText); 158 DocLnk nf(selectedText);
150 nf.removeFiles(); 159 nf.removeFiles();
151 } 160 }
152 } 161 }
153 populateBox(); 162 populateBox();
163 }
154} 164}
155 165
156void NotesControl::slotNewButton() { 166void NotesControl::slotNewButton() {
157 if(edited) save(); 167 if(edited) save();
158 view->clear(); 168 view->clear();
159 view->setFocus(); 169 view->setFocus();
160} 170}
161 171
162void NotesControl::boxPressed(int mouse, QListBoxItem *item, const QPoint&) { 172void NotesControl::boxPressed(int mouse, QListBoxItem *item, const QPoint&) {
163 switch (mouse) { 173 switch (mouse) {
164 case 1:{ 174 case 1:{
165 } 175 }
166 break; 176 break;
167 case 2: 177 case 2:
168 menuTimer.start( 500, TRUE ); 178 menuTimer.start( 500, TRUE );
169 break; 179 break;
170 }; 180 };
171} 181}
172 182
173void NotesControl::slotBoxSelected(const QString &itemString) { 183void NotesControl::slotBoxSelected(const QString &itemString) {
174 if(edited) { 184 if(edited) {
175 save(); 185 save();
176 } 186 }
177 loaded=false; 187 loaded=false;
178 edited=false; 188 edited=false;
179 load(itemString); 189 load(itemString);
180} 190}
181 191
182 192
183void NotesControl::showMenu() { 193void NotesControl::showMenu() {
184 QPopupMenu *m = new QPopupMenu(0); 194 QPopupMenu *m = new QPopupMenu(0);
185 195
186 m->insertItem( tr( "Delete" ), this, SLOT( slotDeleteButton() )); 196 m->insertItem( tr( "Delete" ), this, SLOT( slotDeleteButton() ));
187 197
188 m->setFocus(); 198 m->setFocus();
189 m->exec( QCursor::pos() ); 199 m->exec( QCursor::pos() );
190 200
191 if(m) delete m; 201 if(m) delete m;
192 202
193} 203}
194 204
195void NotesControl::focusOutEvent ( QFocusEvent * e) { 205void NotesControl::focusOutEvent ( QFocusEvent * e) {
196 if( e->reason() == QFocusEvent::Popup) 206 if( e->reason() == QFocusEvent::Popup)
197 save(); 207 save();
198 else { 208 else {
199 if(!loaded) { 209 if(!loaded) {
200 populateBox(); 210 populateBox();
201 load(); 211 load();
202 } 212 }
203 } 213 }
204 QWidget::focusOutEvent(e); 214 QWidget::focusOutEvent(e);
205} 215}
206 216
207void NotesControl::save() { 217void NotesControl::save() {
208 Config cfg("Notes"); 218 Config cfg("Notes");
209 cfg.setGroup("Docs"); 219 cfg.setGroup("Docs");
210 if( edited) { 220 if( edited) {
211 QString rt = view->text(); 221 QString rt = view->text();
212 if(!rt.isEmpty()) { 222 if(!rt.isEmpty()) {
213 QString pt = rt.simplifyWhiteSpace(); 223 QString pt = rt.simplifyWhiteSpace();
214 int i = pt.find( ' ' ); 224 int i = pt.find( ' ' );
215 QString docname = pt; 225 QString docname = pt;
216 if ( i > 0 ) 226 if ( i > 0 )
217 docname = pt.left( i ); 227 docname = pt.left( i );
218 // remove "." at the beginning 228 // remove "." at the beginning
219 while( docname.startsWith( "." ) ) 229 while( docname.startsWith( "." ) )
220 docname = docname.mid( 1 ); 230 docname = docname.mid( 1 );
221 docname.replace( QRegExp("/"), "_" ); 231 docname.replace( QRegExp("/"), "_" );
222 // cut the length. filenames longer than that don't make sense 232 // cut the length. filenames longer than that don't make sense
223 // and something goes wrong when they get too long. 233 // and something goes wrong when they get too long.
224 if ( docname.length() > 40 ) 234 if ( docname.length() > 40 )
225 docname = docname.left(40); 235 docname = docname.left(40);
226 if ( docname.isEmpty() ) 236 if ( docname.isEmpty() )
227 docname = "Empty Text"; 237 docname = "Empty Text";
228 qDebug(docname); 238 qDebug(docname);
229 239
230 if( oldDocName != docname) { 240 if( oldDocName != docname) {
231 int noOfFiles = cfg.readNumEntry("NumberOfFiles", 0 ); 241 int noOfFiles = cfg.readNumEntry("NumberOfFiles", 0 );
232 QString entryName; 242 QString entryName;
233 entryName.sprintf( "File%i", noOfFiles + 1 ); 243 entryName.sprintf( "File%i", noOfFiles + 1 );
234 cfg.writeEntry( entryName,docname ); 244 cfg.writeEntry( entryName,docname );
235 cfg.writeEntry("NumberOfFiles", noOfFiles+1 ); 245 cfg.writeEntry("NumberOfFiles", noOfFiles+1 );
236 cfg.write(); 246 cfg.write();
237 } 247 }
238 doc = new DocLnk(docname); 248 doc = new DocLnk(docname);
239 249
240 doc->setType("text/plain"); 250 doc->setType("text/plain");
241 doc->setFile(docname); 251 doc->setFile(docname);
242 doc->setName(docname); 252 doc->setName(docname);
243 253
244 FileManager fm; 254 FileManager fm;
245 if ( !fm.saveFile( *doc, rt ) ) { 255 if ( !fm.saveFile( *doc, rt ) ) {
246 } 256 }
247 257
248 oldDocName=docname; 258 oldDocName=docname;
249 edited=false; 259 edited=false;
260 qDebug("save");
261 if (doPopulate)
262 populateBox();
250 } 263 }
251 qDebug("save");
252 if (doPopulate)
253 populateBox();
254 }
255 cfg.writeEntry( "LastDoc",oldDocName ); 264 cfg.writeEntry( "LastDoc",oldDocName );
256 cfg.write(); 265 cfg.write();
266
267 }
257} 268}
258 269
259void NotesControl::populateBox() { 270void NotesControl::populateBox() {
260 box->clear(); 271 box->clear();
261 qDebug("populate"); 272 qDebug("populate");
262 Config cfg("Notes"); 273 Config cfg("Notes");
263 cfg.setGroup("Docs"); 274 cfg.setGroup("Docs");
264 int noOfFiles = cfg.readNumEntry("NumberOfFiles", 0 ); 275 int noOfFiles = cfg.readNumEntry("NumberOfFiles", 0 );
265 QStringList list; 276 QStringList list;
266 QString entryName; 277 QString entryName;
267 for ( int i = 0; i < noOfFiles; i++ ) { 278 for ( int i = 0; i < noOfFiles; i++ ) {
268 entryName.sprintf( "File%i", i + 1 ); 279 entryName.sprintf( "File%i", i + 1 );
269 list.append(cfg.readEntry( entryName )); 280 list.append(cfg.readEntry( entryName ));
270 } 281 }
271 list.sort(); 282 list.sort();
272 box->insertStringList(list,-1); 283 box->insertStringList(list,-1);
273 doPopulate=false; 284 doPopulate=false;
274} 285}
275 286
276void NotesControl::load() { 287void NotesControl::load() {
277 288
278 Config cfg("Notes");
279 cfg.setGroup("Docs");
280 if(!loaded) { 289 if(!loaded) {
290 Config cfg("Notes");
291 cfg.setGroup("Docs");
281 QString lastDoc=cfg.readEntry( "LastDoc",""); 292 QString lastDoc=cfg.readEntry( "LastDoc","");
282 DocLnk nf; 293 DocLnk nf;
283 nf.setType("text/plain"); 294 nf.setType("text/plain");
284 nf.setFile(lastDoc); 295 nf.setFile(lastDoc);
285 296
286 loadDoc(nf); 297 loadDoc(nf);
287 loaded=true; 298 loaded=true;
288 oldDocName=lastDoc; 299 oldDocName=lastDoc;
289 } 300 cfg.writeEntry( "LastDoc",oldDocName );
290 cfg.writeEntry( "LastDoc",oldDocName ); 301 cfg.write();
291 cfg.write(); 302 }
292} 303}
293 304
294void NotesControl::load(const QString & file) { 305void NotesControl::load(const QString & file) {
295 qDebug("loading "+file); 306 qDebug("loading "+file);
296 if(!loaded) { 307 if(!loaded) {
297 DocLnk nf; 308 DocLnk nf;
298 nf.setType("text/plain"); 309 nf.setType("text/plain");
299 nf.setFile( file); 310 nf.setFile( file);
300 311
301 loadDoc(nf); 312 loadDoc(nf);
302 loaded=true; 313 loaded=true;
303 } 314 }
304// view->setFocus(); 315// view->setFocus();
305 oldDocName=file; 316 oldDocName=file;
306 Config cfg("Notes"); 317 Config cfg("Notes");
307 cfg.setGroup("Docs"); 318 cfg.setGroup("Docs");
308 cfg.writeEntry( "LastDoc",oldDocName ); 319 cfg.writeEntry( "LastDoc",oldDocName );
309 cfg.write(); 320 cfg.write();
310} 321}
311 322
312void NotesControl::loadDoc( const DocLnk &f) { 323void NotesControl::loadDoc( const DocLnk &f) {
313 FileManager fm; 324 FileManager fm;
314 QString txt; 325 QString txt;
315 if ( !fm.loadFile( f, txt ) ) { 326 if ( !fm.loadFile( f, txt ) ) {
316 return; 327 return;
317 } 328 }
318 view->setText(txt); 329 view->setText(txt);
319} 330}
320 331
321void NotesControl::slotViewEdited() { 332void NotesControl::slotViewEdited() {
322 if(loaded) { 333 if(loaded) {
323 edited=true; 334 edited=true;
324 } 335 }
325} 336}
326 337
327//=========================================================================== 338//===========================================================================
328 339
329NotesApplet::NotesApplet( QWidget *parent, const char *name ) 340NotesApplet::NotesApplet( QWidget *parent, const char *name )
330 : QWidget( parent, name ) { 341 : QWidget( parent, name ) {
331 setFixedHeight( 18 ); 342 setFixedHeight( 18 );
332 setFixedWidth( 14 ); 343 setFixedWidth( 14 );
333 vc = new NotesControl; 344 vc = new NotesControl;
334} 345}
335 346
336NotesApplet::~NotesApplet() { 347NotesApplet::~NotesApplet() {
337} 348}
338 349
339void NotesApplet::mousePressEvent( QMouseEvent *) { 350void NotesApplet::mousePressEvent( QMouseEvent *) {
340 if( !vc->isHidden()) { 351 if( !vc->isHidden()) {
341 vc->doPopulate=false; 352 vc->doPopulate=false;
342 vc->save(); 353 vc->save();
343 vc->close(); 354 vc->close();
344 } else { 355 } else {
345// vc = new NotesControl; 356// vc = new NotesControl;
346// QPoint curPos = mapToGlobal( rect().topLeft() ); 357// QPoint curPos = mapToGlobal( rect().topLeft() );
347 vc->show(); 358 vc->show();
348 vc->move( 5, 18); 359 vc->move( 5, 18);
349 vc->doPopulate=true; 360 vc->doPopulate=true;
350 vc->populateBox(); 361 vc->populateBox();
351 vc->doPopulate=false; 362 vc->doPopulate=false;
363 vc->loaded=false;
352 364
353 vc->load(); 365 vc->load();
354// this->setFocus(); 366// this->setFocus();
355 vc->view->setFocus(); 367 vc->view->setFocus();
356 } 368 }
357} 369}
358 370
359void NotesApplet::paintEvent( QPaintEvent* ) { 371void NotesApplet::paintEvent( QPaintEvent* ) {
360 QPainter p(this); 372 QPainter p(this);
361 p.drawPixmap( 0, 1, ( const char** ) notes_xpm ); 373 p.drawPixmap( 0, 1, ( const char** ) notes_xpm );
362} 374}
363 375