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

TaskSelector.cpp

Go to the documentation of this file.
00001 #include "TaskSelector.h" 
00002 extern QWidget* g_Widget;
00003 
00004 static const char* defkeys = 
00005 "QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
00006 
00007 #define START_INDEX             1
00008 
00009 TaskSelector::TaskSelector(const QString& kind) : m_kind(kind)
00010 {
00011         qDebug("TaskSelector::TaskSelector()");
00012         m_pMenu = new QPopupMenuEx(g_Widget);
00013         m_pMenu->installEventFilter(this);
00014         
00015         m_isShowing = false;
00016         m_index = START_INDEX-1;
00017         connect(m_pMenu, SIGNAL(activated(int)), this, SLOT(select(int)));
00018         connect(m_pMenu, SIGNAL(highlighted(int)), this, SLOT(highlight(int)));
00019 }
00020 
00021 TaskSelector::~TaskSelector()
00022 {
00023         qDebug("TaskSelector::~TaskSelector()");
00024         delete m_pMenu;
00025 }
00026 
00027 bool TaskSelector::onKeyPress(int /*keycode*/)
00028 {
00029         if(m_isShowing){
00030                 qDebug("showing ...");
00031         } else if(m_pMenu->isVisible()){
00032                 next();
00033         } else {
00034                 ConfigEx& cfg = ConfigEx::getInstance("keyhelper");
00035                 cfg.setGroup("Global");
00036                 int delay = cfg.readNumEntry("DelayPopup", 5);
00037                 QTimer::singleShot(delay, this, SLOT(show()));
00038                 m_isShowing = true;
00039         }
00040         return true;
00041 }
00042 
00043 bool TaskSelector::onModRelease(int /*modcode*/)
00044 {
00045         if(m_pMenu->isVisible()){
00046                 //m_pMenu->hide();
00047                 QTimer::singleShot(0, this, SLOT(select()));
00048                 return(true);
00049         } else {
00050                 return(false);
00051         }
00052 }
00053 
00054 int TaskSelector::buildMenu()
00055 {
00056         const AppLnk* lnk;
00057 
00058         ConfigEx& cfg = ConfigEx::getInstance("keyhelper");
00059         QString oldgroup;
00060 
00061         oldgroup = cfg.getGroup();
00062         cfg.setGroup("Global");
00063         QString accesskeys = cfg.readEntry("AccessKeys", defkeys);
00064         if(accesskeys.length() <= 0){
00065                 accesskeys = defkeys;
00066         }
00067         cfg.setGroup(oldgroup);
00068 
00069         /* get list */
00070         int cnt = 0;
00071         m_index = START_INDEX+1;
00072         m_applist.clear();
00073         m_pMenu->clear();
00074         MenuTitle* pTitle = new MenuTitle("TaskSelector", m_pMenu->font(), kind());
00075         m_pMenu->insertItem(pTitle);
00076         const QList<QWSWindow>& list = qwsServer->clientWindows();
00077         QWSWindow* w;
00078         for(QListIterator<QWSWindow> it(list); (w=it.current()); ++it){
00079                 if(w->isVisible() == false
00080                         || w->caption() == QString::null){
00081                         continue;
00082                 }
00083                 QString app = w->client()->identity();
00084                 if(app == NULL || m_applist.contains(app)){
00085                         continue;
00086                 }
00087                 /* exclude "launcher" */
00088                 if(app == "launcher"){
00089                         if(cnt == 0){
00090                                 m_index--;
00091                         }
00092                         continue;
00093                 }
00094                 m_applist.append(app);
00095                 /* append menu */
00096                 cnt++;
00097                 AppLnkSet* lnkSet = AppLnkManager::getInstance();
00098                 lnk = lnkSet->findExec(app);
00099                 QString text;
00100                 QPixmap icon;
00101 #if 0
00102                 if(lnk != NULL){
00103                         icon = lnk->pixmap();
00104                         text = lnk->name();
00105                 } else {
00106                         AppLnkManager::notfound();
00107                         icon = QPixmap();
00108                         text = w->caption();
00109                 }
00110 #else
00111                 if(lnk != NULL){
00112                         icon = lnk->pixmap();
00113                         if(w->caption().length() > 0){
00114                                 text = w->caption();
00115                         } else {
00116                                 text = lnk->name();
00117                         }
00118                 } else {
00119                         AppLnkManager::notfound();
00120                         icon = QPixmap();
00121                         text = w->caption();
00122                 }
00123 #endif
00124                 if(cnt <= (int)accesskeys.length()){
00125                         text.append("(&");
00126                         text.append(accesskeys[cnt-1].upper());
00127                         text.append(")");
00128                 }
00129                 m_pMenu->insertItem(icon, text, cnt);
00130         }
00131         return(cnt);
00132 }
00133 
00134 void TaskSelector::show()
00135 {
00136         /* build task selector menu */
00137         int cnt = buildMenu();
00138         ConfigEx& cfg = ConfigEx::getInstance("keyhelper");
00139         QString oldgroup = cfg.getGroup();
00140         cfg.setGroup("Global");
00141         int min = cfg.readNumEntry("SelectMenuMin", 2);
00142         if(min != 1 && min != 3){
00143                 min = 2;
00144         }
00145         cfg.setGroup(oldgroup);
00146 
00147         if(cnt == 0){
00148                 qDebug("no applications");
00149         } else if(cnt < min){
00150                 //m_index = START_INDEX;
00151                 if(m_index > cnt){
00152                         m_index = cnt;
00153                 }
00154                 QTimer::singleShot(0, this, SLOT(select()));
00155         } else {
00156                 if(m_index > cnt){
00157                         m_index = cnt;
00158                 }
00159                 ConfigEx& cfg = ConfigEx::getInstance("keyhelper");
00160                 cfg.setGroup("Style");
00161                 int x,y;
00162                 QString key = "Position_" + kind();
00163                 if(cfg.hasKey(key)){
00164                         const QStringList& list = cfg.readListEntry(key, ',');
00165                         x = list[0].toInt();
00166                         y = list[1].toInt();
00167                 } else {
00168                         x = (qt_screen->width() - m_pMenu->sizeHint().width()) / 2;
00169                         y = (qt_screen->height() - m_pMenu->sizeHint().height()) / 2;
00170                 }
00171                 QPoint pos(x, y);
00172                 m_pMenu->popup(pos);
00173                 m_pMenu->setActiveItem(m_index);
00174         }
00175         m_isShowing = false;
00176 }
00177 
00178 void TaskSelector::next()
00179 {
00180         m_index++;
00181         if(m_index > (signed int)m_applist.count()){
00182                 m_index = START_INDEX;
00183         }
00184         m_pMenu->setActiveItem(m_index);
00185 }
00186 
00187 void TaskSelector::select()
00188 {
00189         //select(m_index);
00190         m_pMenu->activateItemAt(m_index);
00191 }
00192 
00193 void TaskSelector::select(int index)
00194 {
00195         if(index > 0){
00196                 Global::execute(m_applist[index-1]);
00197         }
00198         m_index = 0;
00199 }
00200 
00201 void TaskSelector::highlight(int index)
00202 {
00203         if(m_pMenu->isVisible()){
00204                 m_index = index;
00205         }
00206 }
00207 
00208 bool TaskSelector::eventFilter(QObject* o, QEvent* e)
00209 {
00210         if(m_pMenu->isVisible()){
00211                 QKeyEvent* ke = (QKeyEvent*)e;
00212                 switch(e->type()){
00213                 case QEvent::Accel:
00214                         if(ke->key() == Qt::Key_Space
00215                                 && ke->isAutoRepeat() == false){
00216                                 select();
00217                         }
00218                         break;
00219                 default:
00220                         //qDebug(">>>>> [%p][%d] <<<<<", o, e->type());
00221                         break;
00222                 }
00223         }
00224         return QObject::eventFilter(o, e);
00225 }

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