Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

configdlg.cpp

Go to the documentation of this file.
00001 /*
00002  * TODO
00003  * make a font selection thing (size too)
00004  * make a keymap editor
00005  * make keys translucent
00006  * make vertical keys possible
00007  *
00008  *
00009  */
00010 
00011 #include <opie2/ofiledialog.h>
00012 #include <opie2/qcolordialog.h>
00013 #include <opie2/oresource.h>
00014 
00015 #include <qpe/qpeapplication.h>
00016 #include <qpe/config.h>
00017 
00018 #include <qlayout.h>
00019 #include <qwidget.h>
00020 #include <qdialog.h>
00021 #include <qtabwidget.h>
00022 #include <qvbox.h>
00023 #include <qgrid.h>
00024 #include <qgroupbox.h>
00025 #include <qlabel.h>
00026 #include <qcheckbox.h>
00027 #include <qsizepolicy.h>
00028 #include <qpushbutton.h>
00029 #include <qlistbox.h>
00030 #include <qstringlist.h>
00031 #include <qtoolbutton.h>
00032 #include <qdir.h>
00033 #include <qfileinfo.h>
00034 #include "configdlg.h"
00035 #include "keyboard.h"
00036 
00037 using namespace Opie;
00038 using namespace Opie::Ui;
00039 // ConfigDlg::ConfigDlg() {{{1
00040 ConfigDlg::ConfigDlg () : QDialog ()
00041 {
00042     setCaption( tr("Multikey Configuration") );
00043     Config config ("multikey");
00044     config.setGroup("keymaps");
00045     QString current_map = config.readEntry("current", 0);
00046 
00047     /*
00048      * 'general config' tab
00049      */
00050 
00051     QVBoxLayout *base_lay = new QVBoxLayout(this);
00052  
00053     QTabWidget *tabs = new QTabWidget(this, "tabs");
00054 
00055     QWidget *gen_box = new QWidget(tabs, "gen_tab");
00056     QVBoxLayout *gen_lay = new QVBoxLayout(gen_box);
00057     gen_lay->setMargin(3);
00058     QGroupBox *map_group = new QGroupBox (2, Qt::Vertical, tr("Keymap File"), gen_box);
00059 
00060     QHBox *hbox1 = new QHBox(map_group);
00061     keymaps = new QListBox(hbox1);
00062     keymaps->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
00063     QVBox *vbox1 = new QVBox(hbox1);
00064     
00065     QToolButton *tb1 = new QToolButton(vbox1, tr("Move Up"));
00066     tb1->setUsesBigPixmap( qApp->desktop()->size().width() > 330 );
00067     tb1->setPixmap(Opie::Core::OResource::loadPixmap("up", Opie::Core::OResource::SmallIcon));
00068     tb1->setAutoRaise(TRUE);
00069     tb1->setFocusPolicy(QWidget::NoFocus);
00070     tb1->setToggleButton(FALSE);
00071     connect(tb1, SIGNAL(clicked()), this, SLOT(moveSelectedUp()));
00072 
00073     QToolButton *tb2 = new QToolButton(vbox1, tr("Move Down"));
00074     tb2->setUsesBigPixmap( qApp->desktop()->size().width() > 330 );
00075     tb2->setPixmap(Opie::Core::OResource::loadPixmap("down", Opie::Core::OResource::SmallIcon));
00076     tb2->setAutoRaise(TRUE);
00077     tb2->setFocusPolicy(QWidget::NoFocus);
00078     tb2->setToggleButton(FALSE);
00079     connect(tb2, SIGNAL(clicked()), this, SLOT(moveSelectedDown()));
00080 
00081     QString cur(tr("Current Language"));
00082     keymaps->insertItem(cur);
00083     keymaps->setSelected(0, true);
00084     
00085     QDir map_dir(QPEApplication::qpeDir() + "share/multikey", "*.keymap");
00086     default_maps = map_dir.entryList(); // so i can access it in other places
00087     custom_maps = config.readListEntry("maps", QChar('|'));
00088     sw_maps = ConfigDlg::loadSw();
00089 
00090     QStringList sw_copy(sw_maps);
00091     for (uint i = 0; i < sw_copy.count(); i++) {
00092 
00093         QString keymap_map;
00094         if (sw_copy[i][0] != '/') { /* share/multikey */
00095 
00096             keymap_map =  map_dir.absPath() + "/" + sw_copy[i];
00097         } else {
00098 
00099             if (map_dir.exists(QFileInfo(sw_copy[i]).fileName(), false) 
00100                 || !QFile::exists(sw_copy[i])) {
00101 
00102                 custom_maps.remove(sw_copy[i]);
00103                 sw_maps.remove(sw_copy[i]);
00104 
00105                 // remove it from the list too
00106                 config.writeEntry("maps", custom_maps.join("|"));
00107 
00108                 continue;
00109             }
00110             keymap_map = sw_copy[i];
00111         }
00112 
00113         QFile map(keymap_map);
00114         if (map.open(IO_ReadOnly)) {
00115 
00116             QString line; bool found = 0;
00117 
00118             map.readLine(line, 1024);
00119             while (!map.atEnd()) {
00120 
00121                 if (line.find(QRegExp("^title\\s*=\\s*")) != -1) {
00122                 
00123                     keymaps->insertItem(line.right(line.length() - line.find(QChar('=')) - 1).stripWhiteSpace());
00124                     found = 1;
00125                     break;
00126                 }
00127                 map.readLine(line, 1024);
00128             }
00129             if (!found)
00130                 keymaps->insertItem(keymap_map);
00131 
00132             map.close();
00133         }
00134 
00135         if (keymap_map == current_map) {
00136             keymaps->setSelected(i + 1, true);
00137         }
00138     }
00139 
00140     // have to "+1" because the "current language" listItem... remember?
00141     connect(keymaps, SIGNAL(highlighted(int)), SLOT(setMap(int)));
00142 
00143     QGrid *add_remove_grid = new QGrid(2, map_group);
00144     add_remove_grid->setMargin(3);
00145     add_remove_grid->setSpacing(3);
00146 
00147     add_button = new QPushButton(tr("Add"), add_remove_grid);
00148     add_button->setFlat(TRUE);
00149     connect(add_button, SIGNAL(clicked()), SLOT(addMap()));
00150 
00151     remove_button = new QPushButton(tr("Remove"), add_remove_grid);
00152     remove_button->setFlat(TRUE);
00153     if (keymaps->currentItem() == 0 || default_maps.find(QFileInfo(current_map).fileName()) != default_maps.end())
00154         remove_button->setDisabled(true);
00155     connect(remove_button, SIGNAL(clicked()), SLOT(removeMap()));
00156 
00157     gen_lay->addWidget(map_group);
00158 
00159     // make a box that will contain the buttons on the bottom
00160     QGrid *other_grid = new QGrid(2, gen_box);
00161     pick_button = new QCheckBox(tr("Pickboard"), other_grid);
00162 
00163     config.setGroup ("general");
00164     bool pick_open = config.readBoolEntry ("usePickboard", FALSE); // default closed
00165     if (pick_open) {
00166 
00167         pick_button->setChecked(true);
00168     }
00169 
00170     repeat_button = new QCheckBox(tr("Key Repeat"), other_grid);
00171     bool repeat_on = config.readBoolEntry ("useRepeat", TRUE);
00172 
00173     if (repeat_on) {
00174 
00175         repeat_button->setChecked(true);
00176     }
00177 
00178     gen_lay->addWidget(other_grid);
00179     tabs->addTab(gen_box, tr("General Settings"));
00180 
00181     /*
00182      * 'color' tab
00183      */
00184 
00185     QWidget *color_box = new QWidget(tabs, "color_tab");
00186     
00187     QGridLayout *color_lay = new QGridLayout(color_box);
00188     QGrid *color_grid = new QGrid(2, color_box);
00189     color_lay->setAlignment(Qt::AlignTop);
00190     color_grid->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
00191     color_grid->layout()->setAlignment(Qt::AlignTop);
00192     color_grid->setMargin(3);
00193     color_grid->setSpacing(3);
00194 
00195     QLabel *label;
00196     QStringList color;
00197     config.setGroup("colors");
00198 
00199     label = new QLabel(tr("Key Color"), color_grid);
00200     keycolor_button = new QPushButton(color_grid);
00201     connect(keycolor_button, SIGNAL(clicked()), SLOT(keyColorClicked()));
00202     keycolor_button->setFlat(TRUE);
00203     color = config.readListEntry("keycolor", QChar(','));
00204     /*
00205      * hopefully not required
00206 
00207     if (color.isEmpty()) {
00208         color = QStringList::split(",", "240,240,240");
00209         config.writeEntry("keycolor", color.join(","));
00210 
00211     }
00212     */
00213     keycolor_button->setPalette(QPalette(QColor(color[0].toInt(), color[1].toInt(), color[2].toInt())));
00214 
00215 
00216     label = new QLabel(tr("Key Pressed Color"), color_grid);
00217     keycolor_pressed_button = new QPushButton(color_grid);
00218     connect(keycolor_pressed_button, SIGNAL(clicked()), SLOT(keyColorPressedClicked()));
00219     keycolor_pressed_button->setFlat(TRUE);
00220     color = config.readListEntry("keycolor_pressed", QChar(','));
00221     keycolor_pressed_button->setPalette(QPalette((QColor(color[0].toInt(), color[1].toInt(), color[2].toInt()))));
00222 
00223     label = new QLabel(tr("Line Color"), color_grid);
00224     keycolor_lines_button = new QPushButton(color_grid);
00225     connect(keycolor_lines_button, SIGNAL(clicked()), SLOT(keyColorLinesClicked()));
00226     keycolor_lines_button->setFlat(TRUE);
00227     color = config.readListEntry("keycolor_lines", QChar(','));
00228     keycolor_lines_button->setPalette(QPalette((QColor(color[0].toInt(), color[1].toInt(), color[2].toInt()))));
00229 
00230 
00231     label = new QLabel(tr("Text Color"), color_grid);
00232     textcolor_button = new QPushButton(color_grid);
00233     connect(textcolor_button, SIGNAL(clicked()), SLOT(textColorClicked()));
00234     textcolor_button->setFlat(TRUE);
00235     color = config.readListEntry("textcolor", QChar(','));
00236     textcolor_button->setPalette(QPalette((QColor(color[0].toInt(), color[1].toInt(), color[2].toInt()))));
00237 
00238     label = new QLabel("", color_grid); // a spacer so the above buttons dont expand
00239     label->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
00240     /* FIXME: hacked spacer height */
00241     QSpacerItem *spacer = new QSpacerItem(0, 300, QSizePolicy::Expanding, QSizePolicy::Maximum);
00242 
00243     color_lay->addWidget(color_grid, 0, 0);
00244     color_lay->addItem(spacer);
00245 
00246     tabs->addTab(color_box, tr("Colors"));
00247     base_lay->addWidget(tabs);
00248 }
00249 
00250 QStringList ConfigDlg::loadSw()
00251 {
00252     Config *config = new Config("multikey");
00253     config->setGroup("keymaps");
00254     QDir map_dir(QPEApplication::qpeDir() + "share/multikey", "*.keymap");
00255     QStringList d_maps = map_dir.entryList(); // so i can access it in other places
00256     QStringList c_maps = config->readListEntry("maps", QChar('|'));
00257     QStringList s_maps = config->readListEntry("sw", QChar('|'));
00258     delete config;
00259 
00260     if (!s_maps.count())
00261     {
00262         s_maps = d_maps+c_maps;
00263     }
00264     else
00265     {
00266         /* Clear non existents entries */
00267         QStringList s_copy(s_maps);
00268         for (uint i = 0; i < s_copy.count(); ++i) {
00269             if (d_maps.find(s_copy[i]) == d_maps.end()
00270                 && c_maps.find(s_copy[i]) == c_maps.end()) {
00271                 s_maps.remove(s_copy[i]);
00272             }
00273         }
00274         /* Update sw_maps from default_maps */
00275         for (uint i = 0; i < d_maps.count(); ++i) {
00276             if (s_maps.find(d_maps[i]) == s_maps.end()) {
00277                 s_maps.append(d_maps[i]);
00278             }
00279         }
00280         /* Update sw_maps from custom_maps */
00281         for (uint i = 0; i < c_maps.count(); ++i) {
00282             if (s_maps.find(c_maps[i]) == s_maps.end()) {
00283                 s_maps.append(c_maps[i]);
00284             }
00285         }
00286     }
00287 
00288     return s_maps;
00289 }
00290 
00291 void ConfigDlg::accept()
00292 {
00293     /* Writing all stuffs to config */
00294     Config *config = new Config("multikey");
00295     config->setGroup("general");
00296     config->writeEntry("usePickboard", pick_button->isChecked()); // default closed
00297     config->writeEntry("useRepeat", repeat_button->isChecked()); // default closed
00298 
00299     config->setGroup("keymaps");
00300     config->writeEntry("sw", sw_maps, QChar('|'));
00301     config->writeEntry("maps", custom_maps, QChar('|'));
00302     delete config;
00303 
00304     int index = keymaps->currentItem();
00305     if (index == 0) {
00306 
00307         remove_button->setDisabled(true);
00308         emit setMapToDefault();
00309     }
00310     else if (default_maps.find(sw_maps[index-1]) != default_maps.end()) {
00311 
00312         remove_button->setDisabled(true);
00313         emit setMapToFile(QPEApplication::qpeDir() + "share/multikey/" + sw_maps[index - 1]);
00314 
00315     } else {
00316 
00317         remove_button->setEnabled(true);
00318         emit setMapToFile(sw_maps[index - 1]);
00319     }
00320 
00321     emit pickboardToggled(pick_button->isChecked());
00322     emit repeatToggled(repeat_button->isChecked());
00323     emit reloadSw();
00324 
00325     QDialog::accept();
00326     emit configDlgClosed();
00327 }
00328 
00329 void ConfigDlg::moveSelectedUp()
00330 {
00331     int i = keymaps->currentItem();
00332     /* Ignore Current Language */
00333     if (i > 1) {
00334         QString t = sw_maps[i-1];
00335         sw_maps[i-1] = sw_maps[i-2];
00336         sw_maps[i-2] = t;
00337 
00338         QString item = keymaps->currentText();
00339         keymaps->removeItem(i);
00340         keymaps->insertItem(item, i-1);
00341         keymaps->setCurrentItem(i-1);
00342     }
00343 }
00344 
00345 void ConfigDlg::moveSelectedDown()
00346 {
00347     int i = keymaps->currentItem();
00348     /* Ignore Current Language */
00349     if (i > 0 && i < (int)keymaps->count() - 1) {
00350         QString t = sw_maps[i-1];
00351         sw_maps[i-1] = sw_maps[i];
00352         sw_maps[i] = t;
00353 
00354         QString item = keymaps->currentText();
00355         keymaps->removeItem(i);
00356         keymaps->insertItem(item, i+1);
00357         keymaps->setCurrentItem(i+1);
00358     }
00359 }
00360 
00361 void ConfigDlg::closeEvent(QCloseEvent *) {
00362 
00363     // tell the parent it was closed, so delete me
00364     emit configDlgClosed();
00365 }
00366 
00367 void ConfigDlg::setMap(int index) {
00368 
00369     if (index == 0 || default_maps.find(sw_maps[index-1]) != default_maps.end()) {
00370         remove_button->setDisabled(true);
00371     } else {
00372         remove_button->setEnabled(true);
00373     }
00374 }
00375 
00376 // ConfigDlg::addMap() {{{1
00377 void ConfigDlg::addMap() {
00378 
00379     QString map = OFileDialog::getOpenFileName(1, QDir::home().absPath());
00380 
00381     if (map.isNull()) return;
00382 
00383     Config config ("multikey");
00384     config.setGroup("keymaps");
00385     QStringList maps = config.readListEntry("maps", QChar('|'));
00386     maps.append(map);
00387     custom_maps.append(map);
00388     if (sw_maps.find(map) == sw_maps.end())
00389         sw_maps.append(map);
00390 
00391     QFile map_file (map);
00392     if (map_file.open(IO_ReadOnly)) {
00393 
00394         QString line; bool found = 0;
00395 
00396         map_file.readLine(line, 1024);
00397         while (!map_file.atEnd()) {
00398 
00399             if (line.find(QRegExp("^title\\s*=\\s*")) != -1) {
00400                 
00401                 keymaps->insertItem(line.right(line.length() - line.find(QChar('=')) - 1).stripWhiteSpace());
00402                 found = 1;
00403                 break;
00404             }
00405             map_file.readLine(line, 1024);
00406         }
00407         if (!found) keymaps->insertItem(map);
00408 
00409         map_file.close();
00410     }
00411 
00412     keymaps->setSelected(keymaps->count() - 1, true);
00413 }
00414 
00415 // ConfigDlg::removeMap() {{{1
00416 void ConfigDlg::removeMap() {
00417 
00418     // move selection up one
00419     keymaps->setSelected(keymaps->currentItem() - 1, true);
00420     // delete the next selected item cus you just moved it up
00421     keymaps->removeItem(keymaps->currentItem() + 1);
00422 
00423     custom_maps.remove(sw_maps[keymaps->currentItem()]);
00424     sw_maps.remove(sw_maps.at(keymaps->currentItem()));
00425 }
00426 
00427 /* ConfigDlg::slots for the color buttons {{{1
00428  *
00429  * these four slots are almost the same, except for the names. i was thinking
00430  * of making a map with pointers to the buttons and names of the configEntry
00431  * so it could be one slot, but then there would be no way of telling which 
00432  * of the buttons was clicked if they all connect to the same slot.
00433  *
00434  */
00435 
00436 void ConfigDlg::keyColorClicked() {
00437 
00438     Config config ("multikey");
00439     config.setGroup ("colors");
00440 
00441     QStringList color = config.readListEntry("keycolor", QChar(','));
00442 
00443     QColor newcolor = QColorDialog::getColor(QColor(color[0].toInt(), color[1].toInt(), color[2].toInt()));
00444 
00445     color[0].setNum(newcolor.red());
00446     color[1].setNum(newcolor.green());
00447     color[2].setNum(newcolor.blue());
00448 
00449     config.writeEntry("keycolor", color, QChar(','));
00450     config.write();
00451     
00452     keycolor_button->setPalette(QPalette(newcolor));
00453     emit reloadKeyboard();
00454 }
00455 void ConfigDlg::keyColorPressedClicked() {
00456 
00457     Config config ("multikey");
00458     config.setGroup ("colors");
00459 
00460     QStringList color = config.readListEntry("keycolor_pressed", QChar(','));
00461 
00462     QColor newcolor = QColorDialog::getColor(QColor(color[0].toInt(), color[1].toInt(), color[2].toInt()));
00463 
00464     color[0].setNum(newcolor.red());
00465     color[1].setNum(newcolor.green());
00466     color[2].setNum(newcolor.blue());
00467 
00468     config.writeEntry("keycolor_pressed", color, QChar(','));
00469     config.write();
00470     
00471     keycolor_pressed_button->setPalette(QPalette(newcolor));
00472     emit reloadKeyboard();
00473 }
00474 void ConfigDlg::keyColorLinesClicked() {
00475 
00476     Config config ("multikey");
00477     config.setGroup ("colors");
00478 
00479     QStringList color = config.readListEntry("keycolor_lines", QChar(','));
00480 
00481     QColor newcolor = QColorDialog::getColor(QColor(color[0].toInt(), color[1].toInt(), color[2].toInt()));
00482 
00483     color[0].setNum(newcolor.red());
00484     color[1].setNum(newcolor.green());
00485     color[2].setNum(newcolor.blue());
00486 
00487     config.writeEntry("keycolor_lines", color, QChar(','));
00488     config.write();
00489     
00490     keycolor_lines_button->setPalette(QPalette(newcolor));
00491     emit reloadKeyboard();
00492 }
00493 void ConfigDlg::textColorClicked() {
00494 
00495     Config config ("multikey");
00496     config.setGroup ("colors");
00497 
00498     QStringList color = config.readListEntry("textcolor", QChar(','));
00499 
00500     QColor newcolor = QColorDialog::getColor(QColor(color[0].toInt(), color[1].toInt(), color[2].toInt()));
00501 
00502     color[0].setNum(newcolor.red());
00503     color[1].setNum(newcolor.green());
00504     color[2].setNum(newcolor.blue());
00505 
00506     config.writeEntry("textcolor", color, QChar(','));
00507     config.write();
00508     
00509     textcolor_button->setPalette(QPalette(newcolor));
00510     emit reloadKeyboard();
00511 }

Generated on Sat Nov 5 16:15:46 2005 for OPIE by  doxygen 1.4.2