~valavanisalex/ubuntu/maverick/scidavis/fix-604811

« back to all changes in this revision

Viewing changes to scidavis/src/MyWidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ruben Molina
  • Date: 2009-09-06 11:34:04 UTC
  • Revision ID: james.westby@ubuntu.com-20090906113404-4awaey82l3686w4q
Tags: upstream-0.2.3
ImportĀ upstreamĀ versionĀ 0.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
    File                 : MyWidget.cpp
 
3
    Project              : SciDAVis
 
4
    Description          : MDI window widget
 
5
    --------------------------------------------------------------------
 
6
    Copyright            : (C) 2006-2009 Knut Franke (knut.franke*gmx.de)
 
7
    Copyright            : (C) 2006-2009 Tilman Benkert (thzs*gmx.net)
 
8
    Copyright            : (C) 2006-2007 by Ion Vasilief (ion_vasilief*yahoo.fr)
 
9
                           (replace * with @ in the email address)
 
10
 
 
11
 ***************************************************************************/
 
12
 
 
13
/***************************************************************************
 
14
 *                                                                         *
 
15
 *  This program is free software; you can redistribute it and/or modify   *
 
16
 *  it under the terms of the GNU General Public License as published by   *
 
17
 *  the Free Software Foundation; either version 2 of the License, or      *
 
18
 *  (at your option) any later version.                                    *
 
19
 *                                                                         *
 
20
 *  This program is distributed in the hope that it will be useful,        *
 
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 
23
 *  GNU General Public License for more details.                           *
 
24
 *                                                                         *
 
25
 *   You should have received a copy of the GNU General Public License     *
 
26
 *   along with this program; if not, write to the Free Software           *
 
27
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
 
28
 *   Boston, MA  02110-1301  USA                                           *
 
29
 *                                                                         *
 
30
 ***************************************************************************/
 
31
#include "MyWidget.h"
 
32
#include "Folder.h"
 
33
 
 
34
#include <QMessageBox>
 
35
#include <QEvent>
 
36
#include <QCloseEvent>
 
37
#include <QString>
 
38
#include <QLocale>
 
39
#include <QIcon>
 
40
#include <QtDebug>
 
41
 
 
42
MyWidget::MyWidget(const QString& label, QWidget * parent, const char * name, Qt::WFlags f):
 
43
        QWidget (parent, f)
 
44
{
 
45
        w_label = label;
 
46
        caption_policy = Both;
 
47
        askOnClose = true;
 
48
        w_status = Normal;
 
49
        titleBar = NULL;
 
50
        setObjectName(QString(name));
 
51
}
 
52
 
 
53
void MyWidget::updateCaption()
 
54
{
 
55
        switch (caption_policy)
 
56
        {
 
57
                case Name:
 
58
                        setWindowTitle(name());
 
59
                        break;
 
60
 
 
61
                case Label:
 
62
                        if (!windowLabel().isEmpty())
 
63
                                setWindowTitle(windowLabel());
 
64
                        else
 
65
                                setWindowTitle(name());
 
66
                        break;
 
67
 
 
68
                case Both:
 
69
                        if (!windowLabel().isEmpty())
 
70
                                setWindowTitle(name() + " - " + windowLabel());
 
71
                        else
 
72
                                setWindowTitle(name());
 
73
                        break;
 
74
        }
 
75
};
 
76
 
 
77
void MyWidget::closeEvent( QCloseEvent *e )
 
78
{
 
79
if (askOnClose)
 
80
    {
 
81
    switch( QMessageBox::information(this,tr("SciDAVis"),
 
82
                                        tr("Do you want to hide or delete") + "<p><b>'" + objectName() + "'</b> ?",
 
83
                                      tr("Delete"), tr("Hide"), tr("Cancel"), 0,2))
 
84
                {
 
85
                case 0:
 
86
                        emit closedWindow(this);
 
87
                        e->accept();
 
88
                break;
 
89
 
 
90
                case 1:
 
91
                        e->ignore();
 
92
                        emit hiddenWindow(this);
 
93
                break;
 
94
 
 
95
                case 2:
 
96
                        e->ignore();
 
97
                break;
 
98
                }
 
99
    }
 
100
else
 
101
    {
 
102
        emit closedWindow(this);
 
103
    e->accept();
 
104
    }
 
105
}
 
106
 
 
107
QString MyWidget::aspect()
 
108
{
 
109
        QString s = tr("Normal");
 
110
        switch (w_status)
 
111
        {
 
112
                case Hidden:
 
113
                        return tr("Hidden");
 
114
                        break;
 
115
 
 
116
                case Normal:
 
117
                        break;
 
118
 
 
119
                case Minimized:
 
120
                        return tr("Minimized");
 
121
                        break;
 
122
 
 
123
                case Maximized:
 
124
                        return tr("Maximized");
 
125
                        break;
 
126
        }
 
127
        return s;
 
128
}
 
129
 
 
130
// Modifying the title bar menu is somewhat more complicated in Qt4.
 
131
// Apart from the trivial change in how we intercept the reparenting,
 
132
// in Qt4 the title bar doesn't exist yet at this point.
 
133
// Thus, we now also have to intercept the creation of the title bar
 
134
// in MyWidget::eventFilter.
 
135
void MyWidget::changeEvent(QEvent *event)
 
136
{
 
137
        if (event->type() == QEvent::ParentChange) {
 
138
                titleBar = 0;
 
139
                if (parent()) parent()->installEventFilter(this);
 
140
        }
 
141
        else if (!isHidden() && event->type() == QEvent::WindowStateChange) {
 
142
            if (((QWindowStateChangeEvent *)event)->oldState() == windowState())
 
143
            return;
 
144
 
 
145
                if( windowState() & Qt::WindowMinimized )
 
146
                w_status = Minimized;
 
147
                else if ( windowState() & Qt::WindowMaximized )
 
148
                w_status = Maximized;
 
149
                else
 
150
                w_status = Normal;
 
151
        emit statusChanged (this);
 
152
        }
 
153
        QWidget::changeEvent(event);
 
154
}
 
155
 
 
156
bool MyWidget::eventFilter(QObject *object, QEvent *e)
 
157
{
 
158
        QWidget *tmp;
 
159
        if (e->type()==QEvent::ContextMenu && object == titleBar)
 
160
        {
 
161
                emit showTitleBarMenu();
 
162
                ((QContextMenuEvent*)e)->accept();
 
163
                return true;
 
164
        } 
 
165
        else if (e->type()==QEvent::ChildAdded && object == parent() && (tmp = qobject_cast<QWidget *>(((QChildEvent*)e)->child()))) 
 
166
        {
 
167
                (titleBar = tmp)->installEventFilter(this);
 
168
                parent()->removeEventFilter(this);
 
169
                return true;
 
170
        }
 
171
        return false;
 
172
}
 
173
 
 
174
void MyWidget::setStatus(Status s)
 
175
{
 
176
        if (w_status == s)
 
177
                return;
 
178
 
 
179
        w_status = s;
 
180
        emit statusChanged (this);
 
181
}
 
182
 
 
183
void MyWidget::setHidden()
 
184
{
 
185
    w_status = Hidden;
 
186
    emit statusChanged (this);
 
187
    hide();
 
188
}
 
189
 
 
190
void MyWidget::setNormal()
 
191
{
 
192
        showNormal();
 
193
        w_status = Normal;
 
194
        emit statusChanged (this);
 
195
}
 
196
 
 
197
void MyWidget::setMinimized()
 
198
{
 
199
        showMinimized();
 
200
        w_status = Minimized;
 
201
        emit statusChanged (this);
 
202
}
 
203
 
 
204
void MyWidget::setMaximized()
 
205
{
 
206
        showMaximized();
 
207
        w_status = Maximized;
 
208
        emit statusChanged (this);
 
209
}
 
210
 
 
211