~ubuntu-branches/debian/sid/smplayer/sid

« back to all changes in this revision

Viewing changes to src/actionseditor.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Breuil Cyril
  • Date: 2007-06-24 16:35:29 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070624163529-hhckbmd24uicada7
Tags: 0.5.20-0ubuntu1
* New upstream release
* Change Maintainer Email

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  smplayer, GUI front-end for mplayer.
 
2
    Copyright (C) 2007 Ricardo Villalba <rvm@escomposlinux.org>
 
3
 
 
4
    This program is free software; you can redistribute it and/or modify
 
5
    it under the terms of the GNU General Public License as published by
 
6
    the Free Software Foundation; either version 2 of the License, or
 
7
    (at your option) any later version.
 
8
 
 
9
    This program is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
    GNU General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU General Public License
 
15
    along with this program; if not, write to the Free Software
 
16
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
*/
 
18
 
 
19
/* This is based on qq14-actioneditor-code.zip from Qt */
 
20
 
1
21
 
2
22
#include "actionseditor.h"
3
23
 
 
24
#if QT_VERSION < 0x040000
 
25
#define USE_KEYCHOOSER 1
 
26
#else
 
27
#define USE_KEYCHOOSER 0
 
28
#endif
 
29
 
 
30
 
 
31
 
4
32
#include <qlayout.h>
5
33
#include <qobjectlist.h>
6
34
#include <qpushbutton.h>
18
46
#include "filedialog.h"
19
47
 
20
48
#include "helper.h"
 
49
#include "mytable.h"
 
50
 
 
51
#if USE_KEYCHOOSER
 
52
#include "keychooser.h"
 
53
#else
 
54
#include <qlineedit.h>
 
55
#endif
 
56
 
 
57
 
 
58
class ShortcutItem : public QTableItem 
 
59
{
 
60
public:
 
61
        ShortcutItem( QTable * table, const QString & text );
 
62
 
 
63
        virtual int alignment () const;
 
64
        virtual QWidget * createEditor () const;
 
65
};
 
66
 
 
67
ShortcutItem::ShortcutItem( QTable * table, const QString & text ) 
 
68
#if USE_KEYCHOOSER
 
69
        : QTableItem(table, QTableItem::WhenCurrent, text)
 
70
#else
 
71
        : QTableItem(table, QTableItem::OnTyping, text)
 
72
#endif
 
73
{
 
74
}
 
75
 
 
76
QWidget * ShortcutItem::createEditor () const {
 
77
#if USE_KEYCHOOSER
 
78
        KeyChooser * key_chooser = new KeyChooser( table()->viewport() );
 
79
        key_chooser->setText( text() );
 
80
        return key_chooser;
 
81
#else
 
82
        QLineEdit * edit = new QLineEdit( table()->viewport() );
 
83
        edit->setText( text() );
 
84
        return edit;
 
85
#endif
 
86
}
 
87
 
 
88
int ShortcutItem::alignment() const {
 
89
        //return Qt::AlignHCenter;
 
90
        return Qt::AlignAuto;
 
91
}
21
92
 
22
93
 
23
94
#define COL_CONFLICTS 0
25
96
#define COL_DESC 2
26
97
#define COL_NAME 3
27
98
 
28
 
class MyTable : public QTable
29
 
{
30
 
public:
31
 
        MyTable( int numRows, int numCols, QWidget * parent = 0, const char * name = 0 );
32
 
        virtual QSize sizeHint () const;
33
 
};
34
 
 
35
 
MyTable::MyTable(int numRows, int numCols, QWidget * parent, const char * name)
36
 
        : QTable(numRows, numCols, parent, name)
37
 
{
38
 
}
39
 
 
40
 
QSize MyTable::sizeHint() const {
41
 
        return QSize(100,100);
42
 
}
43
 
 
44
99
ActionsEditor::ActionsEditor(QWidget * parent, const char * name, WFlags f)
45
100
        : QWidget(parent, name, f)
46
101
{
47
102
        latest_dir = Helper::shortcutsPath();
48
103
 
49
104
    actionsTable = new MyTable(0, COL_NAME +1, this);
 
105
        actionsTable->setSizeHint(QSize(100,100));
50
106
 
51
 
        //actionsTable->setColumnReadOnly(COL_ICON, true);
52
107
        actionsTable->setColumnReadOnly(COL_NAME, true);
53
108
        actionsTable->setColumnReadOnly(COL_DESC, true);
54
109
        actionsTable->setColumnReadOnly(COL_CONFLICTS, true);
82
137
    mainLayout->addWidget(actionsTable);
83
138
    mainLayout->addLayout(buttonLayout);
84
139
 
85
 
        //actionsTable->setResizePolicy(QScrollView::Manual);
86
 
        //actionsTable->resizeContents(100,100);
87
 
        //actionsTable->setVScrollBarMode( QScrollView::AlwaysOn );
88
 
        //actionsTable->viewport()->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Minimum);
89
 
 
90
140
        languageChange();
91
141
}
92
142
 
94
144
}
95
145
 
96
146
void ActionsEditor::languageChange() {
97
 
        //actionsTable->horizontalHeader()->setLabel(COL_ICON, "");
98
147
    actionsTable->horizontalHeader()->setLabel(COL_NAME, tr("Name"));
99
148
    actionsTable->horizontalHeader()->setLabel(COL_DESC, tr("Description"));
100
149
    actionsTable->horizontalHeader()->setLabel(COL_SHORTCUT, tr("Shortcut"));
125
174
        action = static_cast<QACTION*>(actions->first());
126
175
 
127
176
    while (action) {
128
 
        actionsList.append(action);
 
177
                if (!action->inherits("QActionGroup"))
 
178
                actionsList.append(action);
129
179
                action = static_cast<QAction*>(actions->next());
130
180
    }
131
181
#else
132
182
        QList<Q3Action *> actions = widget->findChildren<Q3Action *>();
133
183
        for (int n=0; n < actions.count(); n++) {
134
184
                action = static_cast<QACTION*> (actions[n]);
135
 
        actionsList.append(action);
 
185
                if (!action->inherits("Q3ActionGroup"))
 
186
                actionsList.append(action);
136
187
    }
137
188
#endif
138
189
 
143
194
        actionsTable->setNumRows( actionsList.count() );
144
195
 
145
196
    QACTION *action;
 
197
        QString accelText;
 
198
 
146
199
        for (int n=0; n < actionsList.count(); n++) {
147
200
                action = static_cast<QACTION*> (actionsList[n]);
148
201
                QPixmap p = action->iconSet().pixmap();
149
202
                if (!p.isNull()) p = Images::resize(&p, 16);
 
203
                accelText = QString(action->accel());
150
204
                actionsTable->setPixmap(n, COL_DESC, p );
151
205
                actionsTable->setText(n, COL_NAME, action->name());
152
206
        actionsTable->setText(n, COL_DESC, action->text());
153
 
        actionsTable->setText(n, COL_SHORTCUT, QString(action->accel()));
 
207
        //actionsTable->setText(n, COL_SHORTCUT, QString(action->accel()));
 
208
                actionsTable->setItem( n, COL_SHORTCUT, 
 
209
                               new ShortcutItem(actionsTable, accelText) );
154
210
        }
155
 
        checkForConflicts();
156
 
        //actionsTable->adjustColumn( COL_ICON );
 
211
        hasConflicts(); // Check for conflicts
 
212
 
157
213
        actionsTable->adjustColumn( COL_NAME );
158
214
        actionsTable->adjustColumn( COL_DESC );
159
215
        actionsTable->adjustColumn( COL_CONFLICTS );
189
245
            else
190
246
                item->setText(accelText);
191
247
 
192
 
                checkForConflicts();
193
 
        }
194
 
}
195
 
 
196
 
 
197
 
QACTION * ActionsEditor::findAction(const QString & name) {
198
 
        QACTION *action = 0;
199
 
 
200
 
        for (int n=0; n < actionsList.count(); n++) {
201
 
                action = static_cast<QACTION*> (actionsList[n]);
202
 
                if (name == action->name()) return action;
203
 
        }
204
 
 
205
 
        return action;
206
 
}
 
248
                if (hasConflicts()) qApp->beep();
 
249
        }
 
250
}
 
251
 
 
252
 
207
253
 
208
254
int ActionsEditor::findActionName(const QString & name) {
209
255
        for (int row=0; row < actionsTable->numRows(); row++) {
223
269
        return -1;
224
270
}
225
271
 
226
 
void ActionsEditor::checkForConflicts() {
 
272
bool ActionsEditor::hasConflicts() {
227
273
        int found;
228
274
        bool conflict = false;
229
275
 
235
281
 
236
282
                accelText = actionsTable->text(n, COL_SHORTCUT );
237
283
                if (!accelText.isEmpty()) {
238
 
                        //for (int row=0; row < actionsTable->numRows(); row++) {
239
284
                        found = findActionAccel( accelText, n );
240
285
                        if ( (found != -1) && (found != n) ) {
241
286
                                conflict = true;
244
289
                        }
245
290
                }
246
291
        }
247
 
        if (conflict) qApp->beep();
248
 
}
249
 
 
250
 
void ActionsEditor::saveToConfig(QObject *o, QSettings *set) {
251
 
        qDebug("ActionsEditor::saveToConfig");
252
 
 
253
 
        set->beginGroup("actions");
254
 
 
255
 
        QACTION *action;
256
 
#if QT_VERSION < 0x040000
257
 
        QObjectList *actions = o->queryList("QAction");
258
 
        action = static_cast<QACTION*>(actions->first());
259
 
 
260
 
        while (action) {
261
 
                QString accelText = QString(action->accel());
262
 
                set->writeEntry(action->name(), accelText);
263
 
                action = static_cast<QAction*>(actions->next());
264
 
        }
265
 
#else
266
 
        QList<Q3Action *> actions = o->findChildren<Q3Action *>();
267
 
        for (int n=0; n < actions.count(); n++) {
268
 
                action = static_cast<QACTION*> (actions[n]);
269
 
                QString accelText = QString(action->accel());
270
 
                set->writeEntry(action->name(), accelText);
271
 
    }
272
 
#endif
273
 
 
274
 
        set->endGroup();
275
 
}
276
 
 
277
 
 
278
 
void ActionsEditor::loadFromConfig(QObject *o, QSettings *set) {
279
 
        qDebug("ActionsEditor::loadFromConfig");
280
 
 
281
 
        set->beginGroup("actions");
282
 
 
283
 
        QACTION *action;
284
 
        QString accelText;
285
 
 
286
 
#if QT_VERSION < 0x040000
287
 
        QObjectList *actions = o->queryList("QAction");
288
 
        action = static_cast<QACTION*>(actions->first());
289
 
 
290
 
        while (action) {
291
 
                accelText = set->readEntry(action->name(), action->accel());
292
 
                action->setAccel(QKeySequence(accelText));
293
 
                action = static_cast<QAction*>(actions->next());
294
 
        }
295
 
#else
296
 
        QList<Q3Action *> actions = o->findChildren<Q3Action *>();
297
 
        for (int n=0; n < actions.count(); n++) {
298
 
                action = static_cast<QACTION*> (actions[n]);
299
 
                accelText = set->readEntry(action->name(), action->accel());
300
 
                action->setAccel(QKeySequence(accelText));
301
 
    }
302
 
#endif
303
 
 
304
 
        set->endGroup();
305
 
}
306
 
 
307
 
bool ActionsEditor::saveActionsTable() {
 
292
        //if (conflict) qApp->beep();
 
293
        return conflict;
 
294
}
 
295
 
 
296
 
 
297
void ActionsEditor::saveActionsTable() {
308
298
        QString s = MyFileDialog::getSaveFileName(
309
299
                    latest_dir,
310
300
                    tr("Key files") +" (*.keys)",
326
316
                    QMessageBox::No,
327
317
                    QMessageBox::NoButton);
328
318
                        if (res == QMessageBox::No ) {
329
 
                return false;
 
319
                return;
330
320
                        }
331
321
                }
332
322
                latest_dir = QFileInfo(s).dirPath(TRUE);
333
 
                return saveActionsTable(s);
334
 
        } else {
335
 
                return false;
336
 
        }
 
323
                bool r = saveActionsTable(s);
 
324
                if (!r) {
 
325
                        QMessageBox::warning(this, tr("Error"), 
 
326
               tr("The file couldn't be saved"), 
 
327
               QMessageBox::Ok, QMessageBox::NoButton);
 
328
                }
 
329
        } 
337
330
}
338
331
 
339
332
bool ActionsEditor::saveActionsTable(const QString & filename) {
354
347
        return false;
355
348
}
356
349
 
357
 
bool ActionsEditor::loadActionsTable() {
 
350
void ActionsEditor::loadActionsTable() {
358
351
        QString s = MyFileDialog::getOpenFileName(
359
352
                    latest_dir,
360
353
                    tr("Key files") +" (*.keys)",
364
357
 
365
358
        if (!s.isEmpty()) {
366
359
                latest_dir = QFileInfo(s).dirPath(TRUE);
367
 
                return loadActionsTable(s);
 
360
                bool r = loadActionsTable(s);
 
361
                if (!r) {
 
362
                        QMessageBox::warning(this, tr("Error"), 
 
363
               tr("The file couldn't be loaded"), 
 
364
               QMessageBox::Ok, QMessageBox::NoButton);
 
365
                }
368
366
        }
369
 
        return false;
370
367
}
371
368
 
372
369
bool ActionsEditor::loadActionsTable(const QString & filename) {
398
395
                        }
399
396
                }
400
397
                f.close();
401
 
                checkForConflicts();
 
398
                hasConflicts(); // Check for conflicts
402
399
                return true;
403
400
        } else {
404
401
                return false;
405
402
        }
406
403
}
407
404
 
 
405
 
 
406
// Static functions
 
407
 
 
408
void ActionsEditor::saveToConfig(QObject *o, QSettings *set) {
 
409
        qDebug("ActionsEditor::saveToConfig");
 
410
 
 
411
        set->beginGroup("actions");
 
412
 
 
413
        QACTION *action;
 
414
#if QT_VERSION < 0x040000
 
415
        QObjectList *actions = o->queryList("QAction");
 
416
        action = static_cast<QACTION*>(actions->first());
 
417
 
 
418
        while (action) {
 
419
                QString accelText = QString(action->accel());
 
420
                set->writeEntry(action->name(), accelText);
 
421
                action = static_cast<QAction*>(actions->next());
 
422
        }
 
423
#else
 
424
        QList<Q3Action *> actions = o->findChildren<Q3Action *>();
 
425
        for (int n=0; n < actions.count(); n++) {
 
426
                action = static_cast<QACTION*> (actions[n]);
 
427
                QString accelText = QString(action->accel());
 
428
                set->writeEntry(action->name(), accelText);
 
429
    }
 
430
#endif
 
431
 
 
432
        set->endGroup();
 
433
}
 
434
 
 
435
 
 
436
void ActionsEditor::loadFromConfig(QObject *o, QSettings *set) {
 
437
        qDebug("ActionsEditor::loadFromConfig");
 
438
 
 
439
        set->beginGroup("actions");
 
440
 
 
441
        QACTION *action;
 
442
        QString accelText;
 
443
 
 
444
#if QT_VERSION < 0x040000
 
445
        QObjectList *actions = o->queryList("QAction");
 
446
        action = static_cast<QACTION*>(actions->first());
 
447
 
 
448
        while (action) {
 
449
                accelText = set->readEntry(action->name(), action->accel());
 
450
                action->setAccel(QKeySequence(accelText));
 
451
                action = static_cast<QAction*>(actions->next());
 
452
        }
 
453
#else
 
454
        QList<Q3Action *> actions = o->findChildren<Q3Action *>();
 
455
        for (int n=0; n < actions.count(); n++) {
 
456
                action = static_cast<QACTION*> (actions[n]);
 
457
                accelText = set->readEntry(action->name(), action->accel());
 
458
                action->setAccel(QKeySequence(accelText));
 
459
    }
 
460
#endif
 
461
 
 
462
        set->endGroup();
 
463
}
 
464
 
 
465
QACTION * ActionsEditor::findAction(QObject *o, const QString & name) {
 
466
        QACTION *action;
 
467
 
 
468
#if QT_VERSION < 0x040000
 
469
        QObjectList *actions = o->queryList("QAction");
 
470
        action = static_cast<QACTION*>(actions->first());
 
471
 
 
472
    while (action) {
 
473
                if (name == action->name()) return action;
 
474
                action = static_cast<QAction*>(actions->next());
 
475
    }
 
476
#else
 
477
        QList<Q3Action *> actions = o->findChildren<Q3Action *>();
 
478
        for (int n=0; n < actions.count(); n++) {
 
479
                action = static_cast<QACTION*> (actions[n]);
 
480
                if (name == action->objectName()) return action;
 
481
    }
 
482
#endif
 
483
 
 
484
        return 0;
 
485
}
 
486
 
 
487
QStringList ActionsEditor::actionsNames(QObject *o) {
 
488
        QStringList l;
 
489
 
 
490
        QACTION *action;
 
491
 
 
492
#if QT_VERSION < 0x040000
 
493
        QObjectList *actions = o->queryList("QAction");
 
494
        action = static_cast<QACTION*>(actions->first());
 
495
 
 
496
    while (action) {
 
497
                l.append( action->name() );
 
498
                action = static_cast<QAction*>(actions->next());
 
499
    }
 
500
#else
 
501
        QList<Q3Action *> actions = o->findChildren<Q3Action *>();
 
502
        for (int n=0; n < actions.count(); n++) {
 
503
                action = static_cast<QACTION*> (actions[n]);
 
504
                l.append( action->name() );
 
505
    }
 
506
#endif
 
507
 
 
508
        return l;
 
509
}
 
510