~ubuntu-branches/ubuntu/trusty/bibletime/trusty

« back to all changes in this revision

Viewing changes to src/frontend/cmdiarea.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Marsden
  • Date: 2010-01-10 22:21:36 UTC
  • mfrom: (1.3.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20100110222136-905hza76230hperg
Tags: 2.5-1
New upstream version 2.5 (Closes: #564551).

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
#include <QMdiSubWindow>
15
15
#include <QTimer>
16
16
#include <QWindowStateChangeEvent>
 
17
#include <QMenu>
17
18
 
18
19
 
19
20
CMDIArea::CMDIArea(BibleTime *parent)
20
 
        : QMdiArea(parent), m_mdiArrangementMode(ArrangementModeManual)
21
 
{
 
21
        : QMdiArea(parent), m_mdiArrangementMode(ArrangementModeManual) {
22
22
    Q_ASSERT(parent != 0);
23
23
 
24
24
    setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
28
28
            this, SLOT(slotSubWindowActivated(QMdiSubWindow*)));
29
29
}
30
30
 
 
31
static const int moveSize = 30;
 
32
 
31
33
QMdiSubWindow* CMDIArea::addSubWindow(QWidget * widget, Qt::WindowFlags windowFlags) {
32
34
    QMdiSubWindow* subWindow = QMdiArea::addSubWindow(widget, windowFlags);
33
35
    subWindow->installEventFilter(this);
34
36
 
35
 
    //If we do have a maximized Window, set it to normal so that the new window can be seen
36
 
    if (activeSubWindow() && activeSubWindow()->isMaximized()) {
37
 
        activeSubWindow()->showNormal();
 
37
    // Change Qt QMdiSubWindow Close action to have no shortcut
 
38
    // This makes our closeWindow actions with Ctrl-W work correctly
 
39
    QList<QAction*> actions = subWindow->systemMenu()->actions();
 
40
    for (int i=0; i<actions.count(); i++) {
 
41
        QAction* action = actions.at(i);
 
42
        QString text = action->text();
 
43
        if (text.contains("Close")) {
 
44
            action->setShortcut(QKeySequence());
 
45
            break;
 
46
        }
38
47
    }
39
48
 
 
49
    // Manual arrangement mode
 
50
    enableWindowMinMaxFlags(true);
40
51
    if (m_mdiArrangementMode == ArrangementModeManual) {
41
 
        subWindow->resize(400, 400); //set the window to be big enough
 
52
        // Note that the window size/maximization may be changed later by a session restore.
 
53
        // If we already have an active window, make the new one simular to it
 
54
        if (activeSubWindow()) {
 
55
            if (activeSubWindow()->isMaximized()) {
 
56
                // Maximize the new window
 
57
                subWindow->showMaximized();
 
58
            }
 
59
            else {
 
60
                // Make new window the same size as the active window and move it slightly.
 
61
                subWindow->resize(activeSubWindow()->size());
 
62
                QRect subWinGeom = activeSubWindow()->geometry();
 
63
                subWinGeom.translate(moveSize, moveSize);
 
64
                // If it goes off screen, move it almost to the top left
 
65
                if ( ! frameRect().contains(subWinGeom)) {
 
66
                    subWinGeom.moveTo(moveSize, moveSize);
 
67
                }
 
68
                subWindow->setGeometry(subWinGeom);
 
69
            }
 
70
        }
 
71
        else {
 
72
            //set the window to be big enough
 
73
            subWindow->resize(400, 400);
 
74
        }
42
75
        subWindow->raise();
43
76
    }
44
77
    else {
 
78
        // Automatic arrangement modes
 
79
        enableWindowMinMaxFlags(false);
45
80
        triggerWindowUpdate();
46
81
    }
47
82
    return subWindow;
58
93
    }
59
94
 
60
95
    QList<QMdiSubWindow*> windows = usableWindowList();
61
 
    if (activeSubWindow() && activeSubWindow()->isMaximized()) {
62
 
        if (activeSubWindow()->size() != this->size()) {
63
 
            activeSubWindow()->resize(this->size());
64
 
        }
65
 
    }
66
 
    else if (windows.count() == 1) {
67
 
        windows.at(0)->showMaximized();
68
 
    }
69
 
    else {
70
 
        setUpdatesEnabled(false);
71
 
        QMdiSubWindow* active = activeSubWindow();
72
 
 
73
 
        const int widthForEach = width() / windows.count();
74
 
        unsigned int x = 0;
75
 
        foreach (QMdiSubWindow *window, windows) {
76
 
            window->showNormal();
77
 
 
78
 
            const int preferredWidth = window->minimumWidth() + window->baseSize().width();
79
 
            const int actWidth = qMax(widthForEach, preferredWidth);
80
 
 
81
 
            window->setGeometry(x, 0, actWidth, height());
82
 
            x += actWidth;
83
 
        }
84
 
 
85
 
        if (active) active->setFocus();
86
 
        setUpdatesEnabled(true);
87
 
    }
88
 
    emitWindowCaptionChanged();
 
96
    setUpdatesEnabled(false);
 
97
    QMdiSubWindow* active = activeSubWindow();
 
98
 
 
99
    const int widthForEach = width() / windows.count();
 
100
    unsigned int x = 0;
 
101
    foreach (QMdiSubWindow *window, windows) {
 
102
        window->showNormal();
 
103
 
 
104
        const int preferredWidth = window->minimumWidth() + window->baseSize().width();
 
105
        const int actWidth = qMax(widthForEach, preferredWidth);
 
106
 
 
107
        window->setGeometry(x, 0, actWidth, height());
 
108
        x += actWidth;
 
109
    }
 
110
 
 
111
    if (active) active->setFocus();
 
112
    setUpdatesEnabled(true);
 
113
emitWindowCaptionChanged();
89
114
}
90
115
 
91
116
void CMDIArea::myTileHorizontal() {
94
119
    }
95
120
 
96
121
    QList<QMdiSubWindow*> windows = usableWindowList();
97
 
 
98
 
    if (activeSubWindow() && activeSubWindow()->isMaximized()) {
99
 
        if (activeSubWindow()->size() != this->size()) {
100
 
            activeSubWindow()->resize(this->size());
101
 
        }
102
 
    }
103
 
    else if (windows.count() == 1) {
104
 
        windows.at(0)->showMaximized();
105
 
    }
106
 
    else {
107
 
        setUpdatesEnabled(false);
108
 
        QMdiSubWindow* active = activeSubWindow();
109
 
 
110
 
        const int heightForEach = height() / windows.count();
111
 
        unsigned int y = 0;
112
 
        foreach (QMdiSubWindow *window, windows) {
113
 
            window->showNormal();
114
 
 
115
 
            const int preferredHeight = window->minimumHeight() + window->baseSize().height();
116
 
            const int actHeight = qMax(heightForEach, preferredHeight);
117
 
 
118
 
            window->setGeometry( 0, y, width(), actHeight );
119
 
            y += actHeight;
120
 
        }
121
 
        if (active) active->setFocus();
122
 
        setUpdatesEnabled(true);
123
 
    }
 
122
    setUpdatesEnabled(false);
 
123
    QMdiSubWindow* active = activeSubWindow();
 
124
 
 
125
    const int heightForEach = height() / windows.count();
 
126
    unsigned int y = 0;
 
127
    foreach (QMdiSubWindow *window, windows) {
 
128
        window->showNormal();
 
129
 
 
130
        const int preferredHeight = window->minimumHeight() + window->baseSize().height();
 
131
        const int actHeight = qMax(heightForEach, preferredHeight);
 
132
 
 
133
        window->setGeometry( 0, y, width(), actHeight );
 
134
        y += actHeight;
 
135
    }
 
136
    if (active) active->setFocus();
 
137
    setUpdatesEnabled(true);
 
138
    emitWindowCaptionChanged();
 
139
}
 
140
 
 
141
void CMDIArea::myTile() {
 
142
    if (!updatesEnabled() || !usableWindowList().count() ) {
 
143
        return;
 
144
    }
 
145
    tileSubWindows();
124
146
    emitWindowCaptionChanged();
125
147
}
126
148
 
131
153
 
132
154
    QList<QMdiSubWindow*> windows = usableWindowList();
133
155
 
134
 
    if (activeSubWindow() && activeSubWindow()->isMaximized()) {
135
 
        if (activeSubWindow()->size() != this->size()) {
136
 
            activeSubWindow()->resize(this->size());
137
 
        }
138
 
    }
139
 
    else if (windows.count() == 1) {
 
156
    if (windows.count() == 1) {
140
157
        windows.at(0)->showMaximized();
141
158
    }
142
159
    else {
155
172
            if (window == active) { //leave out the active window which should be the top window
156
173
                continue;
157
174
            }
 
175
            window->showNormal();
158
176
            window->raise(); //make it the on-top-of-window-stack window to make sure they're in the right order
159
177
            window->setGeometry(x, y, windowWidth, windowHeight);
160
178
            x += offsetX;
161
179
            y += offsetY;
162
180
        }
 
181
        active->showNormal();
163
182
        active->setGeometry(x, y, windowWidth, windowHeight);
164
183
        active->raise();
165
184
        active->activateWindow();
172
191
void CMDIArea::emitWindowCaptionChanged() {
173
192
    if (activeSubWindow()) {
174
193
        emit sigSetToplevelCaption(activeSubWindow()->windowTitle());
175
 
    } else {
 
194
    }
 
195
    else {
176
196
        emit sigSetToplevelCaption(QString());
177
197
    }
178
198
}
182
202
    //in subWindowList() when their ChildAdded-Event is triggered
183
203
    QList<QMdiSubWindow*> ret;
184
204
    foreach(QMdiSubWindow* w, subWindowList()) {
185
 
        if (w->isMinimized() || w->isHidden()) { //not usable for us
 
205
        if (w->isHidden()) { //not usable for us
186
206
            continue;
187
207
        }
188
208
        ret.append( w );
197
217
    emit sigSetToplevelCaption( client->windowTitle().trimmed() );
198
218
}
199
219
 
200
 
//resize event of the MDI area itself, update layout if necessary
201
220
void CMDIArea::resizeEvent(QResizeEvent* e) {
202
 
    // Do not call QMdiArea::resizeEvent(e), this would mess up our layout
203
 
        // unless we are in manual mode
204
 
    if (updatesEnabled()) triggerWindowUpdate();
205
 
        if (m_mdiArrangementMode == ArrangementModeManual)
206
 
            QMdiArea::resizeEvent(e);
 
221
    /*
 
222
      Do not call QMdiArea::resizeEvent(e) unless we are in manual arrangement
 
223
      mode, since this would mess up our layout. The manual arrangement mode
 
224
      should be handled exactly as in QMdiArea.
 
225
    */
 
226
    if (m_mdiArrangementMode == ArrangementModeManual) {
 
227
        QMdiArea::resizeEvent(e);
 
228
    }
 
229
    else if (updatesEnabled()) {
 
230
        triggerWindowUpdate();
 
231
    }
207
232
}
208
233
 
209
234
//handle events of the client windows to update layout if necessary
214
239
    if (w == 0) return QMdiArea::eventFilter(o, e);
215
240
 
216
241
    switch (e->type()) {
217
 
        case QEvent::WindowStateChange:
218
 
        {
 
242
        case QEvent::WindowStateChange: {
219
243
            Qt::WindowStates newState(w->windowState());
220
244
            Qt::WindowStates oldState(((QWindowStateChangeEvent*)e)->oldState());
221
245
 
241
265
            triggerWindowUpdate();
242
266
            break;
243
267
        case QEvent::WindowTitleChange:
244
 
            emitWindowCaptionChanged();
 
268
            if (o == activeSubWindow()) {
 
269
                emit sigSetToplevelCaption(w->windowTitle());
 
270
            }
245
271
            break;
246
272
        default:
247
273
            break;
262
288
            case ArrangementModeCascade:
263
289
                QTimer::singleShot(0, this, SLOT(myCascade()));
264
290
                break;
 
291
            case ArrangementModeTile:
 
292
                QTimer::singleShot(0, this, SLOT(myTile()));
 
293
                break;
265
294
            default:
266
295
                break;
267
296
        }
268
297
    }
269
298
}
 
299
 
 
300
void CMDIArea::enableWindowMinMaxFlags(bool enable)
 
301
{
 
302
    foreach(QMdiSubWindow* subWindow, subWindowList()) {
 
303
        Qt::WindowFlags flags = subWindow->windowFlags();
 
304
        if (enable) {
 
305
            flags |= Qt::WindowMinimizeButtonHint;
 
306
            flags |= Qt::WindowMaximizeButtonHint;
 
307
        }
 
308
        else {
 
309
            flags &= ~Qt::WindowMinimizeButtonHint;
 
310
            flags &= ~Qt::WindowMaximizeButtonHint;
 
311
        }
 
312
        subWindow->setWindowFlags(flags);
 
313
        subWindow->hide();
 
314
        subWindow->show();
 
315
    }
 
316
}
 
317