~ubuntu-branches/ubuntu/wily/psi/wily

« back to all changes in this revision

Viewing changes to src/mainwin_p.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-08-28 18:46:52 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080828184652-iiik12dl91nq7cdi
Tags: 0.12-2
Uploading to unstable (Closes: Bug#494352)

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *
19
19
 */
20
20
 
21
 
#include "common.h"
22
21
#include "mainwin_p.h"
23
22
 
24
23
#include <qapplication.h>
25
24
#include <qstyle.h>
26
 
#include <q3toolbar.h>
 
25
#include <QToolBar>
27
26
#include <qtimer.h>
28
27
#include <qsignalmapper.h>
29
28
#include <qobject.h>
35
34
#include <QLabel>
36
35
#include <QMenu>
37
36
#include <QMouseEvent>
 
37
#include <QtAlgorithms>
 
38
 
38
39
#include "psiaccount.h"
39
40
#include "stretchwidget.h"
40
41
#include "iconwidget.h"
55
56
 
56
57
        void setIcon(PsiIcon *, bool showText);
57
58
        void setLabel(QString);
 
59
 
 
60
        // reimplemented
58
61
        QSize sizeHint() const;
 
62
        QSize minimumSizeHint() const;
59
63
 
60
64
private slots:
61
65
        void pixmapUpdated();
89
93
        return QPushButton::sizeHint();
90
94
}
91
95
 
 
96
QSize PopupActionButton::minimumSizeHint() const
 
97
{
 
98
        return QSize(16, 16);
 
99
}
 
100
 
92
101
void PopupActionButton::setIcon(PsiIcon *i, bool st)
93
102
{
94
103
        if ( icon ) {
116
125
 
117
126
void PopupActionButton::update()
118
127
{
119
 
        if ( showText )
 
128
        if(qVersionInt() >= 0x040300)
 
129
        {
 
130
                if((showText && !label.isEmpty()) && styleSheet().isEmpty()) {
 
131
                        setStyleSheet("text-align: left");
 
132
                }
 
133
                else if((!showText || label.isEmpty()) && styleSheet() == "text-align: left") {
 
134
                        setStyleSheet(QString());
 
135
                }
 
136
        }
 
137
 
 
138
        if (showText) {
120
139
                QPushButton::setText(label);
121
 
        else
 
140
        } else {
122
141
                QPushButton::setText("");
 
142
        }
123
143
}
124
144
 
125
145
void PopupActionButton::pixmapUpdated()
289
309
        }
290
310
}
291
311
 
292
 
bool PopupAction::addTo (QWidget *w)
 
312
bool PopupAction::addTo(QWidget *w)
293
313
{
294
 
        if ( w->inherits("QToolBar") || w->inherits("Q3ToolBar") ) {
295
 
                QByteArray bname((const char*) (QString(name()) + QString("_action_button")));
296
 
                PopupActionButton *btn = new PopupActionButton ( (Q3ToolBar*)w, bname );
297
 
                d->buttons.append ( btn );
298
 
                btn->setMenu ( menu() );
299
 
                btn->setLabel ( text() );
300
 
                btn->setIcon ( d->icon, d->showText );
301
 
                btn->setSizePolicy ( d->size );
302
 
                btn->setEnabled ( isEnabled() );
 
314
        QToolBar* toolbar = dynamic_cast<QToolBar*>(w);
 
315
        if (toolbar) {
 
316
                QByteArray bname((const char*)(QString(name()) + QString("_action_button")));
 
317
                PopupActionButton *btn = new PopupActionButton(w, bname);
 
318
                d->buttons.append(btn);
 
319
                btn->setMenu(menu());
 
320
                btn->setLabel(text());
 
321
                btn->setIcon(d->icon, d->showText);
 
322
                btn->setSizePolicy(d->size);
 
323
                btn->setEnabled(isEnabled());
 
324
                toolbar->addWidget(btn);
303
325
 
304
 
                connect( btn, SIGNAL( destroyed() ), SLOT( objectDestroyed() ) );
 
326
                connect(btn, SIGNAL(destroyed()), SLOT(objectDestroyed()));
305
327
        }
306
 
        else
 
328
        else {
307
329
                return IconAction::addTo(w);
 
330
        }
308
331
 
309
332
        return true;
310
333
}
375
398
// MAction
376
399
//----------------------------------------------------------------------------
377
400
 
378
 
class MAction::Private : public QObject
379
 
{
380
 
public:
381
 
        int id;
382
 
        PsiCon *psi;
383
 
        QSignalMapper *sm;
384
 
 
385
 
        Private (int _id, PsiCon *_psi, QObject *parent)
386
 
        : QObject (parent)
387
 
        {
388
 
                id = _id;
389
 
                psi = _psi;
390
 
                sm = new QSignalMapper(this, "MAction::Private::SignalMapper");
391
 
        }
392
 
 
393
 
        QMenu *subMenu(QWidget *p)
394
 
        {
395
 
                QMenu *pm = new QMenu (p);
396
 
                uint i = 0;
397
 
                foreach(PsiAccount* acc, psi->contactList()->enabledAccounts()) {
398
 
                        pm->insertItem( acc->name(), parent(), SLOT(itemActivated(int)), 0, id*1000 + i );
399
 
                        pm->setItemParameter ( id*1000 + i, i );
400
 
                        i++;
401
 
                }
402
 
                return pm;
403
 
        }
404
 
 
405
 
        void updateToolButton(QToolButton *btn)
406
 
        {
407
 
                if (psi->contactList()->enabledAccounts().count() >= 2) {
408
 
                        btn->setMenu(subMenu(btn));
409
 
                        disconnect(btn, SIGNAL(clicked()), sm, SLOT(map()));
410
 
                }
411
 
                else {
412
 
                        btn->setMenu(0);
413
 
                        // connect exactly once:
414
 
                        disconnect(btn, SIGNAL(clicked()), sm, SLOT(map()));
415
 
                        connect(btn, SIGNAL(clicked()), sm, SLOT(map()));
416
 
                }
417
 
        }
418
 
};
419
 
 
420
 
MAction::MAction(PsiIcon i, const QString &s, int id, PsiCon *psi, QObject *parent)
421
 
: IconAction(s, s, 0, parent)
422
 
{
423
 
        init (i, id, psi);
424
 
}
425
 
 
426
 
MAction::MAction(const QString &s, int id, PsiCon *psi, QObject *parent)
427
 
: IconAction(s, s, 0, parent)
428
 
{
429
 
        init (PsiIcon(), id, psi);
430
 
}
431
 
 
432
 
void MAction::init(PsiIcon i, int id, PsiCon *psi)
433
 
{
434
 
        d = new Private(id, psi, this);
435
 
        setPsiIcon (&i);
436
 
        connect(psi, SIGNAL(accountCountChanged()), SLOT(numAccountsChanged()));
437
 
        setEnabled ( d->psi->contactList()->haveEnabledAccounts() );
438
 
        connect (d->sm, SIGNAL(mapped(int)), SLOT(itemActivated(int)));
439
 
}
440
 
 
441
 
bool MAction::addTo(QWidget *w)
442
 
{
443
 
        if ( w->inherits("QPopupMenu") || w->inherits("QMenu") )
444
 
        {
445
 
                QMenu *menu = (QMenu*)w;
446
 
        QIcon iconset;
447
 
#ifndef Q_WS_MAC
448
 
                iconset = iconSet();
449
 
#endif
450
 
                if ( d->psi->contactList()->enabledAccounts().count() < 2 ) {
451
 
                        menu->insertItem ( iconset, menuText(), this, SLOT(itemActivated(int)), 0, d->id*1000 + 0 );
452
 
                        menu->setItemEnabled (d->id*1000 + 0, isEnabled());
453
 
                        menu->setItemParameter ( d->id*1000 + 0, 0 );
454
 
                }
455
 
                else
456
 
                        menu->insertItem(iconset, menuText(), d->subMenu(w));
457
 
        }
458
 
        else
459
 
                return IconAction::addTo(w);
460
 
 
 
401
MAction::MAction(PsiIcon i, const QString& s, int id, PsiCon* psi, QObject* parent)
 
402
        : IconActionGroup(parent)
 
403
{
 
404
        init(s, i, id, psi);
 
405
}
 
406
 
 
407
MAction::MAction(const QString& s, int id, PsiCon* psi, QObject* parent)
 
408
        : IconActionGroup(parent)
 
409
{
 
410
        init(s, PsiIcon(), id, psi);
 
411
}
 
412
 
 
413
void MAction::init(const QString& name, PsiIcon i, int id, PsiCon* psi)
 
414
{
 
415
        id_ = id;
 
416
        controller_ = psi;
 
417
 
 
418
        setText(name);
 
419
        setStatusTip(name);
 
420
        setPsiIcon(&i);
 
421
 
 
422
        connect(controller_, SIGNAL(accountCountChanged()), SLOT(numAccountsChanged()));
 
423
        connect(this, SIGNAL(activated()), SLOT(slotActivated()));
 
424
        numAccountsChanged();
 
425
}
 
426
 
 
427
bool MAction::addTo(QWidget* widget)
 
428
{
 
429
        widget->addAction(this);
461
430
        return true;
462
431
}
463
432
 
464
 
void MAction::addingToolButton(IconToolButton *btn)
465
 
{
466
 
        d->sm->setMapping(btn, 0);
467
 
 
468
 
        d->updateToolButton(btn);
469
 
}
470
 
 
471
 
void MAction::itemActivated(int n)
472
 
{
473
 
        QList<PsiAccount*> list = d->psi->contactList()->enabledAccounts();
474
 
 
475
 
        if (n >= list.count()) // just in case
476
 
                return;
477
 
 
478
 
        emit activated((PsiAccount *)list.at(n), d->id);
 
433
QList<PsiAccount*> MAction::accounts() const
 
434
{
 
435
        return controller_->contactList()->enabledAccounts();
 
436
}
 
437
 
 
438
void MAction::slotActivated()
 
439
{
 
440
        if (accounts().count() > 0) {
 
441
                emit activated(accounts().first(), id_);
 
442
        }
 
443
}
 
444
 
 
445
void MAction::actionActivated()
 
446
{
 
447
        QAction* action = static_cast<QAction*>(sender());
 
448
        int num = action->property("id").toInt();
 
449
        if (num >= 0 && num < accounts().count()) {
 
450
                emit activated(accounts().at(num), id_);
 
451
        }
479
452
}
480
453
 
481
454
void MAction::numAccountsChanged()
482
455
{
483
 
        setEnabled( d->psi->contactList()->haveEnabledAccounts() );
484
 
 
485
 
        QList<IconToolButton*> btns = buttonList();
486
 
        for ( QList<IconToolButton*>::Iterator it = btns.begin(); it != btns.end(); ++it ) {
487
 
                QToolButton *btn = (*it);
488
 
 
489
 
                if (btn->menu())
490
 
                        delete btn->menu();
491
 
 
492
 
                d->updateToolButton(btn);
 
456
        setEnabled(accounts().count() > 0);
 
457
 
 
458
        qDeleteAll(findChildren<QAction*>());
 
459
 
 
460
        foreach(PsiAccount* account, accounts()) {
 
461
                QAction* act = new QAction(account->name(), this);
 
462
                act->setProperty("id", accounts().indexOf(account));
 
463
                connect(act, SIGNAL(activated()), SLOT(actionActivated()));
493
464
        }
494
465
}
495
466
 
496
467
IconAction *MAction::copy() const
497
468
{
498
 
        MAction *act = new MAction(text(), d->id, d->psi, 0);
 
469
        MAction *act = new MAction(text(), id_, controller_, 0);
499
470
 
500
471
        *act = *this;
501
472
 
509
480
        return *this;
510
481
}
511
482
 
 
483
void MAction::doSetMenu(QMenu* menu)
 
484
{
 
485
        IconActionGroup::doSetMenu(findChildren<QAction*>().count() > 1 ? menu : 0);
 
486
}
 
487
 
512
488
//----------------------------------------------------------------------------
513
489
// SpacerAction
514
490
//----------------------------------------------------------------------------
527
503
 
528
504
bool SpacerAction::addTo(QWidget *w)
529
505
{
530
 
        if ( w->inherits("QToolBar") || w->inherits("Q3ToolBar") ) {
531
 
                new StretchWidget(w);
 
506
        QToolBar* toolbar = dynamic_cast<QToolBar*>(w);
 
507
        if (toolbar) {
 
508
                StretchWidget* stretch = new StretchWidget(w);
 
509
                toolbar->addWidget(stretch);
532
510
                return true;
533
511
        }
534
512
 
579
557
 
580
558
        Q3PtrList<MLabel> labels;
581
559
        bool hide;
 
560
        QString message;
582
561
};
583
562
 
584
563
EventNotifierAction::EventNotifierAction(QObject *parent, const char *name)
585
564
: IconAction(parent, name)
586
565
{
587
566
        d = new Private;
588
 
        setMenuText(tr("<Event notifier>"));
 
567
        setText(tr("<Event notifier>"));
589
568
        d->hide = true;
590
569
}
591
570
 
596
575
 
597
576
bool EventNotifierAction::addTo(QWidget *w)
598
577
{
599
 
        if ( w->inherits("QToolBar") || w->inherits("Q3ToolBar") ) {
 
578
        if (w) {
600
579
                MLabel *label = new MLabel(w, "EventNotifierAction::MLabel");
601
 
                label->setText(text());
 
580
                label->setText(d->message);
602
581
                d->labels.append(label);
603
582
                connect(label, SIGNAL(destroyed()), SLOT(objectDestroyed()));
604
583
                connect(label, SIGNAL(doubleClicked()), SIGNAL(activated()));
605
584
                connect(label, SIGNAL(clicked(int)), SIGNAL(clicked(int)));
606
585
 
607
 
                if ( d->hide )
 
586
                QToolBar* toolbar = dynamic_cast<QToolBar*>(w);
 
587
                if (!toolbar) {
 
588
                        QLayout* layout = w->layout();
 
589
                        if (layout)
 
590
                                layout->addWidget(label);
 
591
                }
 
592
                else {
 
593
                        toolbar->addWidget(label);
 
594
                }
 
595
 
 
596
                if (d->hide)
608
597
                        hide();
609
598
 
610
599
                return true;
613
602
        return false;
614
603
}
615
604
 
616
 
void EventNotifierAction::setText(const QString &t)
 
605
void EventNotifierAction::setMessage(const QString &m)
617
606
{
618
 
        IconAction::setText("<nobr>" + t + "</nobr>");
 
607
        d->message = m;
619
608
 
620
609
        Q3PtrListIterator<MLabel> it ( d->labels );
621
610
        for ( ; it.current(); ++it) {
622
611
                MLabel *label = it.current();
623
 
                label->setText(text());
 
612
                label->setText(d->message);
624
613
        }
625
614
}
626
615
 
634
623
{
635
624
        d->hide = true;
636
625
 
637
 
        Q3PtrListIterator<MLabel> it ( d->labels );
638
 
        for ( ; it.current(); ++it) {
 
626
        Q3PtrListIterator<MLabel> it(d->labels);
 
627
        for (; it.current(); ++it) {
639
628
                MLabel *label = it.current();
640
629
                label->hide();
641
 
                Q3ToolBar *toolBar = (Q3ToolBar *)label->parent();
642
 
 
643
 
                QObjectList l = toolBar->queryList( "QWidget" );
644
 
                int found = 0;
645
 
 
646
 
                for ( QObjectList::ConstIterator it = l.begin(); it != l.end(); ++it) {
647
 
                        if ( QString((*it)->name()).left(3) != "qt_" ) // misc internal Qt objects
648
 
                                found++;
 
630
                QToolBar *toolBar = dynamic_cast<QToolBar*>(label->parent());
 
631
                if (toolBar) {
 
632
                        int found = 0;
 
633
                        foreach(QWidget* widget, toolBar->findChildren<QWidget*>()) {
 
634
                                if (!widget->objectName().startsWith("qt_") &&
 
635
                                    !QString(widget->metaObject()->className()).startsWith("QToolBar"))
 
636
                                {
 
637
                                        found++;
 
638
                                }
 
639
                        }
 
640
 
 
641
                        if (found == 1)   // only MLabel is on ToolBar
 
642
                                toolBar->hide();
649
643
                }
650
 
 
651
 
                if ( found == 1 ) // only MLabel is on ToolBar
652
 
                        toolBar->hide();
653
644
        }
654
645
}
655
646
 
660
651
        Q3PtrListIterator<MLabel> it ( d->labels );
661
652
        for ( ; it.current(); ++it) {
662
653
                MLabel *label = it.current();
663
 
                Q3ToolBar *toolBar = (Q3ToolBar *)label->parent();
664
654
                label->show();
665
 
                toolBar->show();
 
655
                QToolBar *toolBar = dynamic_cast<QToolBar*>(label->parent());
 
656
                if (toolBar)
 
657
                        toolBar->show();
666
658
        }
667
659
}
668
660
 
 
661
void EventNotifierAction::updateVisibility()
 
662
{
 
663
        if (d->hide)
 
664
                hide();
 
665
        else
 
666
                show();
 
667
}
 
668
 
669
669
IconAction *EventNotifierAction::copy() const
670
670
{
671
671
        EventNotifierAction *act = new EventNotifierAction( 0 );