File: IceMainWidget.cpp

package info (click to toggle)
icemc 0.2.4-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 264 kB
  • ctags: 246
  • sloc: cpp: 2,211; makefile: 43
file content (459 lines) | stat: -rw-r--r-- 13,683 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/**
 * IceMC - a menu editor for IceWM
 * Copyright (c) 2000 Georg Mittendorfer
 */

///////////////////////////////////////////////////////
// IceMainWidget.cpp 
// Implementation of class IceMainWidget
// Stores the main data for IceMainWindow
// Declaration -> IceMainWindow.h
//
// created:  mig 00090?
// modified: mig 030222
///////////////////////////////////////////////////////

#include "IceMainWindow.h"
#include "IceFileSearch.h"
#include "IceListView.h"
#include "IceIconPreview.h"

#include <qcombobox.h>
#include <qfiledialog.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qmessagebox.h>
#include <qpainter.h>
#include <qpixmap.h>
#include <qpushbutton.h>
#include <qregexp.h>

#include "fileopen.xpm"

//#include "IceMainWindow.moc"

///////////////////////////////////////////////////////
// constructor & destructor
///////////////////////////////////////////////////////

IceMainWidget::IceMainWidget(QWidget * parent, const char * name, WFlags f) : QWidget (parent,name,f)
{
  // constructor: QWidget ( QWidget * parent=0, const char * name=0, WFlags f=0 )

  homeDir = std::getenv("HOME");
  setDefaultPaths();

  /* 3 textediting fields + labels */
  applEdit = new QLineEdit(this);
  iconEdit = new QLineEdit(this);
  execEdit = new QLineEdit(this);
  applEdit->setMinimumSize(180,25);
  iconEdit->setMinimumSize(180,25);
  execEdit->setMinimumSize(180,25);
  QLabel* applLabel = new QLabel(applEdit,"&Name",this);
  QLabel* iconLabel = new QLabel(iconEdit,"&Icon",this);
  QLabel* execLabel = new QLabel(execEdit,"&Exec",this);

  QPushButton* execPb = new QPushButton(this);
  QPushButton* iconPb = new QPushButton(this);
  execPb->setPixmap(fileopen);
  execPb->setMinimumSize(25,25);
  execPb->setMaximumWidth(25); 
  iconPb->setPixmap(fileopen);
  iconPb->setMinimumSize(25,25);
  execPb->setMaximumWidth(25);

  infoCombo = new QComboBox(false,this);
  infoCombo->setMinimumSize(85,25);
  infoCombo->setMaximumHeight(25);
  infoCombo->insertItem("menu");
  infoCombo->insertItem("prog");
  infoCombo->insertItem("separator");
  infoCombo->insertItem("restart");
  infoCombo->insertItem("menufile");
  infoCombo->insertItem("menuprog");

  mainview = new IceListView(this);
  mainview->addColumn("Application Name",120);
  mainview->addColumn("Icon",120);
  mainview->addColumn("Executable",250);
  mainview->setColumnWidthMode(0,IceListView::Maximum);
  mainview->setColumnWidthMode(2,IceListView::Maximum);
  mainview->setSorting(-1);
  mainview->setRootIsDecorated(true);
  mainview->setAllColumnsShowFocus(true);
  mainview->setSelectionMode(IceListView::Single);
  mainview->setMouseTracking(false);
  
  connect(mainview, SIGNAL(currentChanged(QListViewItem*)), this, SLOT(displayListItem(QListViewItem*)));
  connect(applEdit, SIGNAL(textChanged(const QString&)), this, SLOT(updateListAppl(const QString&)));
  connect(iconEdit, SIGNAL(textChanged(const QString&)), this, SLOT(updateListIcon(const QString&)));
  connect(execEdit, SIGNAL(textChanged(const QString&)), this, SLOT(updateListExec(const QString&)));
  connect(infoCombo, SIGNAL(activated(const QString&)), this, SLOT(updateInfoStr(const QString&)));
  connect(execPb, SIGNAL(clicked()), this, SLOT(newExec()));
  connect(iconPb, SIGNAL(clicked()), this, SLOT(newIcon()));

  /* layout */
  QVBoxLayout* mainLayout = new QVBoxLayout(this,2);
  QGridLayout* editLayout = new QGridLayout(5,6,10); // 5 rows 6 coloumns 10pix spacing

  editLayout->addWidget(applLabel,2,1,Qt::AlignRight);
  editLayout->addWidget(iconLabel,3,1,Qt::AlignRight);
  editLayout->addWidget(execLabel,4,1,Qt::AlignRight);
  editLayout->addWidget(applEdit,2,2);
  editLayout->addWidget(iconEdit,3,2);
  editLayout->addWidget(execEdit,4,2);
  editLayout->addMultiCellWidget(infoCombo,2,2,4,5);
  editLayout->addWidget(iconPb,3,4,Qt::AlignLeft);
  editLayout->addWidget(execPb,4,4,Qt::AlignLeft);
  editLayout->addColSpacing(3,10);
  editLayout->addColSpacing(6,20);
  editLayout->addRowSpacing(1,8);
  editLayout->addRowSpacing(5,2);

  mainLayout->addWidget(mainview);
  mainLayout->addLayout(editLayout,0);
  
  //  QPalette temp = new QPalette
  p = new QPainter(mainview); // or 'this'?
  cg = &((mainview->palette()).active()); // maybe this doesn't work => check
}

IceMainWidget::~IceMainWidget()
{
  delete p;
  // Qt does the rest of the job (i hope ;)
}

/////////////////////////////////////////////////////
// public static data
/////////////////////////////////////////////////////

QString IceMainWidget::homeDir = 0;
QString IceMainWidget::libPath = 0;

/////////////////////////////////////////////////////
// public methods
/////////////////////////////////////////////////////

void IceMainWidget::clearLineEdits()
{
  applEdit->clear();
  iconEdit->clear();
  execEdit->clear();
}

void IceMainWidget::setDefaultPaths()
{
  setLibPath();
  setIconPath();
  IceListView::path = std::getenv("PATH");
}

void IceMainWidget::setLibPath(QString userPath)
{
  libPath = homeDir + "/.icewm:/etc/X11/icewm:/usr/local/lib/X11/icewm"
    + ":/usr/X11R6/lib/X11/icewm:/usr/X11/lib/X11/icewm";
  if (!userPath.isEmpty())
    libPath = userPath + ":" + libPath;
}

void IceMainWidget::setIconPath(QString userPath)
{
  QString fName;
  QString iconPathTemp;
  QString iconPath = homeDir + "/.icewm:/etc/X11/icewm/icons:/usr/local/lib/X11/icewm/icons"
    + ":/usr/X11R6/lib/X11/icewm/icons:/usr/X11/lib/X11/icewm/icons";
  if (!userPath.isEmpty())
    iconPath = iconPath + ":" + userPath;
  else {
    IceFileSearch* search = new IceFileSearch();
    fName = search->fileReadable("preferences",iconPath);
    if (fName != "") { 
      // search preferences file for iconPath:
      // iconPathTemp = searchPref(fName,"IconPath");
      // if (!(iconPathTemp.isEmpty()))
      //   iconPath = iconPath + ":" + iconPathTemp; 
      //
    }
    delete search;
  }
  // data stored in icelistview! (ev change this?)
  IceListView::iconPath = iconPath;
}

QString IceMainWidget::getLibPath()
{
  return libPath;
}

QString IceMainWidget::getIconPath()
{
  return IceListView::iconPath;
}

QString IceMainWidget::getHomeDir()
{
  return homeDir;
}

void IceMainWidget::updateListItem(IceListViewItem* item, bool all)
{
  ;//qDebug("DEBUG: IceMainWidget::updateListItem");
  QString info;
  if (item) {
    mainview->setCurrentItem(item);
    ;//qDebug("DEBUG: updateItem: " + item->text(0));
    info = item->getInfo();
    // for all:
    updateListIcon(item->text(1));
    updateListExec(item->text(2));
    if (info == "separator")
      updateListAppl(" ------ ");
    if (all) // update all positions (signal should be sufficient)
      updateListAppl(item->text(0));
  }
  else
    qWarning("WARNING: null pointer (IceMainWidget::updateItem)");
}

/////////////////////////////////////////////////////
// public slots
/////////////////////////////////////////////////////

void IceMainWidget::displayListItem(QListViewItem* qItem)
{
  if (qItem) {

    // no update of QListView needed if only current item changed (update folder() -vs- performance)
    disconnect(applEdit, SIGNAL(textChanged(const QString&)), this, SLOT(updateListAppl(const QString&)));
    disconnect(iconEdit, SIGNAL(textChanged(const QString&)), this, SLOT(updateListIcon(const QString&)));
    disconnect(execEdit, SIGNAL(textChanged(const QString&)), this, SLOT(updateListExec(const QString&)));

    IceListViewItem* item;
    QString info;
    // typesafe cast QListViewItem to IceListViewItem
    item = dynamic_cast<IceListViewItem*> (qItem);

    ;//qDebug("DEBUG: displayListItem: " + item->text(0));	
    
    applEdit->setEnabled(true);
    iconEdit->setEnabled(true);
    execEdit->setEnabled(true);  
    
    applEdit->setText(item->text(0));
    iconEdit->setText(item->text(1));
    execEdit->setText(item->text(2));    

    info = item->getInfo();

    connect(applEdit, SIGNAL(textChanged(const QString&)), this, SLOT(updateListAppl(const QString&)));
    connect(iconEdit, SIGNAL(textChanged(const QString&)), this, SLOT(updateListIcon(const QString&)));
    connect(execEdit, SIGNAL(textChanged(const QString&)), this, SLOT(updateListExec(const QString&)));

    if ((info == "menu") || (info == "separator")) {
      execEdit->setEnabled(false);
      infoCombo->setCurrentItem(0);
      if (info == "separator") {
	applEdit->setEnabled(false);
	iconEdit->setEnabled(false);
	infoCombo->setCurrentItem(2);
      }
    }
    else {
      infoCombo->setEnabled(true);
      if (info == "prog")
	infoCombo->setCurrentItem(1);
      else if (info == "restart")
	infoCombo->setCurrentItem(3);
      else if (info == "menufile")
	infoCombo->setCurrentItem(4);
      else if (info == "menuprog")
	infoCombo->setCurrentItem(5);
      else {
	popupMessage("... to err is human ...","WARNING - No Type Selected\n"
		     "Please select appropriate Type for Item\n");
	qWarning("WARNING: IceMainWidget::displayListItem; unknown info string" + info);
      }
    }
  }
  else
    ;//qDebug("DEBUG: IceMainWidget::displayListItem(...): null pointer");
    // happens i.e. if cut() on last item
}

void IceMainWidget::updateListAppl(const QString& appl)
{
  IceListViewItem* item;
  item = mainview->currentItem();
  if (item) {
    ;//qDebug("DEBUG: updateListAppl: " + item->text(0));
    item->setText(0,appl);
  }
}

void IceMainWidget::updateListIcon(const QString& icon)
{
  IceListViewItem* item;
  item = mainview->currentItem();
  if (item) {
    ;//qDebug("DEBUG: updateListIcon: " + item->text(0));
    item->setText(1,icon);
    item->setIcon();
  }
}

void IceMainWidget::updateListExec(const QString& exec)
{
  IceListViewItem* item;
  item = mainview->currentItem();

  if (item) {
    ;//qDebug("DEBUG: updateListExec: " + item->text(0));
    item->setText(2,exec);
    if (item->getInfo() == "menu") {
      item->setExecState();
    }
    else if (item->getInfo() == "prog" || item->getInfo() == "restart") {
      item->setExecState();
      mainview->updateRootFolder(item); // this is a performance problem!
    }
  }
}

void IceMainWidget::updateInfoStr(const QString& selected)
{
  IceListViewItem* item;
  QString info;
  item = mainview->currentItem();

  if (item) {
    ;//qDebug("DEBUG: updateInfoStr: " + selected + " " + info + " " + item->text(0));
    info = item->getInfo();
#ifdef DEBUG
    cout << "updateInfoStr: info=" << info << "; selected=" << selected << endl;
#endif
    if (info != selected) {
      if (info == "menu") {
	if (item->firstChild()) {
	 popupMessage("Error", "Folder not empty!");
	 infoCombo->setCurrentItem(0);
	}
        else {
	  item->setInfo(selected);
	  item->setExpandable(false);
	  updateListItem(item);
	  displayListItem(item);
	}
      }
      else {
	item->setInfo(selected);
	if (selected == "menu")
	  item->setExpandable(true);
	else
	  item->setExpandable(false);
	updateListItem(item);
	displayListItem(item);
      }
    }
  }
}

/**
 * Select an executable using a file dialog
 */
void IceMainWidget::newExec()
{
  ;//qDebug("DEBUG: IceMainWidget::newExec()");
  QString file;
  int ok;
  static QString startDir = homeDir;

  QFileDialog* dialog = new QFileDialog(startDir,QString::null,this,"newExec",true);
  dialog->setShowHiddenFiles(true);
  dialog->setMode(QFileDialog::ExistingFile);
  dialog->setCaption("Please Select Application");
  ok = dialog->exec();

  file = dialog->selectedFile();
  if ( (!file.isEmpty()) && (ok) ) {
    if (execEdit->isEnabled())
      execEdit->setText(file);
    else
      popupMessage("... to err is human ...",
		   "Application Executable Field is not enabled.\n"
		   "Please select appropriate Type for Item\n"
		   "(e.g. 'prog' instead of 'separator' or 'menu')");
  }
  else {
    // canceled
  }

  startDir = dialog->dirPath(); // for next use
  delete dialog;
}

/**
 * Select a new icon by using a file dialog
 */
void IceMainWidget::newIcon()
{
  // TODO: implement a subclass of QFileIconProvider for QFileDialog
  ;//qDebug("DEBUG: IceMainWidget::newIcon()");
  QString icon;
  int ok;
  static QString startDir = homeDir;
  QString filterTypes("XPM Icons (*.xpm);;IceWM Menu Icons (*_16x16.xpm);;All Files (*.*)");

  IceIconPreview *p = new IceIconPreview(); 
  QFileDialog *dialog = new QFileDialog(startDir,QString::null,this,"newExec",true);
  dialog->setShowHiddenFiles(true);
  //dialog->addFilter("All Files (*.*)"); // not compatible with qt 2.3
//    dialog->addFilter("XPM Icons (*.xpm)"); // not compatible with qt 2.3
//    dialog->addFilter("IceWM Icons (*_16x16.xpm)"); // not compatible with qt 2.3
  dialog->setFilters(filterTypes);
  dialog->setMode(QFileDialog::ExistingFile);
  dialog->setCaption("Please Select Icon");
  dialog->setContentsPreviewEnabled(true);
  dialog->setContentsPreview(p,p);
  dialog->setPreviewMode(QFileDialog::Contents);
  ok = dialog->exec();

  icon = dialog->selectedFile();
  if ( (!icon.isEmpty()) && (ok) ) {
    if (iconEdit->isEnabled()) {
      icon = icon.replace( QRegExp("_..x..\\.xpm"), "" );
      iconEdit->setText(icon);
    }
    else
      popupMessage("... to err is human ...","Icon Field is not enabled.\n"
		   "Please select appropriate Type for Item\n"
		   "(e.g. 'prog' instead of 'separator')");
  }
  else {
    // canceled
  }

  startDir = dialog->dirPath(); // for next use  
  delete dialog;
}

/////////////////////////////////////////////////////////////////
// private methods
/////////////////////////////////////////////////////////////////

void IceMainWidget::popupMessage(QString caption, QString msg)
{
  QMessageBox::warning( this, caption, msg, "Ok");
  //  QMessageBox warnBox(caption, msg, QMessageBox::Warning, QMessageBox::Ok | QMessageBox::Default, 0, 0);
  //  warnBox.show(); 
}