OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / coreplugin / outputpane.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "outputpane.h"
35 #include "outputpanemanager.h"
36
37 #include "coreconstants.h"
38 #include "icore.h"
39 #include "ioutputpane.h"
40 #include "modemanager.h"
41
42 #include <QtGui/QSplitter>
43 #include <QtGui/QVBoxLayout>
44
45 namespace Core {
46
47 struct OutputPanePlaceHolderPrivate {
48     explicit OutputPanePlaceHolderPrivate(Core::IMode *mode, QSplitter *parent);
49
50     Core::IMode *m_mode;
51     QSplitter *m_splitter;
52     bool m_closeable;
53     static OutputPanePlaceHolder* m_current;
54 };
55
56 OutputPanePlaceHolderPrivate::OutputPanePlaceHolderPrivate(Core::IMode *mode, QSplitter *parent) :
57     m_mode(mode), m_splitter(parent), m_closeable(true)
58 {
59 }
60
61 OutputPanePlaceHolder *OutputPanePlaceHolderPrivate::m_current = 0;
62
63 OutputPanePlaceHolder::OutputPanePlaceHolder(Core::IMode *mode, QSplitter* parent)
64    : QWidget(parent), d(new OutputPanePlaceHolderPrivate(mode, parent))
65 {
66     setVisible(false);
67     setLayout(new QVBoxLayout);
68     QSizePolicy sp;
69     sp.setHorizontalPolicy(QSizePolicy::Preferred);
70     sp.setVerticalPolicy(QSizePolicy::Preferred);
71     sp.setHorizontalStretch(0);
72     setSizePolicy(sp);
73     layout()->setMargin(0);
74     connect(Core::ModeManager::instance(), SIGNAL(currentModeChanged(Core::IMode *)),
75             this, SLOT(currentModeChanged(Core::IMode *)));
76 }
77
78 OutputPanePlaceHolder::~OutputPanePlaceHolder()
79 {
80     if (d->m_current == this) {
81         // FIXME: Prevent exit crash in debug mode.
82         if (Internal::OutputPaneManager *om = Internal::OutputPaneManager::instance()) {
83             om->setParent(0);
84             om->hide();
85         }
86     }
87 }
88
89 void OutputPanePlaceHolder::setCloseable(bool b)
90 {
91     d->m_closeable = b;
92 }
93
94 bool OutputPanePlaceHolder::closeable()
95 {
96     return d->m_closeable;
97 }
98
99 void OutputPanePlaceHolder::currentModeChanged(Core::IMode *mode)
100 {
101     if (d->m_current == this) {
102         d->m_current = 0;
103         Internal::OutputPaneManager *om = Internal::OutputPaneManager::instance();
104         om->setParent(0);
105         om->hide();
106         om->updateStatusButtons(false);
107     }
108     if (d->m_mode == mode) {
109         d->m_current = this;
110         Internal::OutputPaneManager *om = Internal::OutputPaneManager::instance();
111         layout()->addWidget(om);
112         om->show();
113         om->updateStatusButtons(isVisible());
114         om->setCloseable(d->m_closeable);
115     }
116 }
117
118 void OutputPanePlaceHolder::maximizeOrMinimize(bool maximize)
119 {
120     if (!d->m_splitter)
121         return;
122     int idx = d->m_splitter->indexOf(this);
123     if (idx < 0)
124         return;
125
126     QList<int> sizes = d->m_splitter->sizes();
127
128     if (maximize) {
129         int sum = 0;
130         foreach(int s, sizes)
131             sum += s;
132         for (int i = 0; i < sizes.count(); ++i) {
133             sizes[i] = 32;
134         }
135         sizes[idx] = sum - (sizes.count()-1) * 32;
136     } else {
137         int target = sizeHint().height();
138         int space = sizes[idx] - target;
139         if (space > 0) {
140             for (int i = 0; i < sizes.count(); ++i) {
141                 sizes[i] += space / (sizes.count()-1);
142             }
143             sizes[idx] = target;
144         }
145     }
146
147     d->m_splitter->setSizes(sizes);
148
149 }
150
151 bool OutputPanePlaceHolder::isMaximized() const
152 {
153     return Internal::OutputPaneManager::instance()->isMaximized();
154 }
155
156 void OutputPanePlaceHolder::unmaximize()
157 {
158     if (Internal::OutputPaneManager::instance()->isMaximized())
159         Internal::OutputPaneManager::instance()->slotMinMax();
160 }
161
162 OutputPanePlaceHolder *OutputPanePlaceHolder::getCurrent()
163 {
164     return OutputPanePlaceHolderPrivate::m_current;
165 }
166
167 bool OutputPanePlaceHolder::canMaximizeOrMinimize() const
168 {
169     return d->m_splitter != 0;
170 }
171
172 bool OutputPanePlaceHolder::isCurrentVisible()
173 {
174     return OutputPanePlaceHolderPrivate::m_current && OutputPanePlaceHolderPrivate::m_current->isVisible();
175 }
176
177 } // namespace Core
178
179