OSDN Git Service

653eaea7d0d2f56f9e7aaaea504b84d0f38eb091
[qt-creator-jp/qt-creator-jp.git] / src / plugins / coreplugin / rightpane.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 "rightpane.h"
35
36 #include <coreplugin/modemanager.h>
37
38 #include <QtCore/QSettings>
39
40 #include <QtGui/QVBoxLayout>
41 #include <QtGui/QSplitter>
42 #include <QtGui/QResizeEvent>
43 #include <QtGui/QTextEdit>
44
45
46 using namespace Core;
47 using namespace Core::Internal;
48
49 RightPanePlaceHolder *RightPanePlaceHolder::m_current = 0;
50
51 RightPanePlaceHolder* RightPanePlaceHolder::current()
52 {
53     return m_current;
54 }
55
56 RightPanePlaceHolder::RightPanePlaceHolder(Core::IMode *mode, QWidget *parent)
57     :QWidget(parent), m_mode(mode)
58 {
59     setLayout(new QVBoxLayout);
60     layout()->setMargin(0);
61     connect(Core::ModeManager::instance(), SIGNAL(currentModeChanged(Core::IMode *)),
62             this, SLOT(currentModeChanged(Core::IMode *)));
63 }
64
65 RightPanePlaceHolder::~RightPanePlaceHolder()
66 {
67     if (m_current == this) {
68         RightPaneWidget::instance()->setParent(0);
69         RightPaneWidget::instance()->hide();
70     }
71 }
72
73 void RightPanePlaceHolder::applyStoredSize(int width)
74 {
75     if (width) {
76         QSplitter *splitter = qobject_cast<QSplitter *>(parentWidget());
77         if (splitter) {
78             // A splitter we need to resize the splitter sizes
79             QList<int> sizes = splitter->sizes();
80             int index = splitter->indexOf(this);
81             int diff = width - sizes.at(index);
82             int adjust = sizes.count() > 1 ? (diff / (sizes.count() - 1)) : 0;
83             for (int i = 0; i < sizes.count(); ++i) {
84                 if (i != index)
85                     sizes[i] -= adjust;
86             }
87             sizes[index]= width;
88             splitter->setSizes(sizes);
89         } else {
90             QSize s = size();
91             s.setWidth(width);
92             resize(s);
93         }
94     }
95 }
96
97 // This function does work even though the order in which
98 // the placeHolder get the signal is undefined.
99 // It does ensure that after all PlaceHolders got the signal
100 // m_current points to the current PlaceHolder, or zero if there
101 // is no PlaceHolder in this mode
102 // And that the parent of the RightPaneWidget gets the correct parent
103 void RightPanePlaceHolder::currentModeChanged(Core::IMode *mode)
104 {
105     if (m_current == this) {
106         m_current = 0;
107         RightPaneWidget::instance()->setParent(0);
108         RightPaneWidget::instance()->hide();
109     }
110     if (m_mode == mode) {
111         m_current = this;
112
113         int width = RightPaneWidget::instance()->storedWidth();
114
115         layout()->addWidget(RightPaneWidget::instance());
116         RightPaneWidget::instance()->show();
117
118         applyStoredSize(width);
119         setVisible(RightPaneWidget::instance()->isShown());
120     }
121 }
122
123 /////
124 // RightPaneWidget
125 /////
126
127
128 RightPaneWidget *RightPaneWidget::m_instance = 0;
129
130 RightPaneWidget::RightPaneWidget()
131     : m_shown(true), m_width(0)
132 {
133     m_instance = this;
134
135     QVBoxLayout *layout = new QVBoxLayout;
136     layout->setMargin(0);
137     setLayout(layout);
138 }
139
140 RightPaneWidget::~RightPaneWidget()
141 {
142     clearWidget();
143     m_instance = 0;
144 }
145
146 RightPaneWidget *RightPaneWidget::instance()
147 {
148     return m_instance;
149 }
150
151 void RightPaneWidget::setWidget(QWidget *widget)
152 {
153     clearWidget();
154     m_widget = widget;
155     if (m_widget) {
156         m_widget->setParent(this);
157         layout()->addWidget(m_widget);
158         setFocusProxy(m_widget);
159         m_widget->show();
160     }
161 }
162
163 int RightPaneWidget::storedWidth()
164 {
165     return m_width;
166 }
167
168 void RightPaneWidget::resizeEvent(QResizeEvent *re)
169 {
170     if (m_width && re->size().width())
171         m_width = re->size().width();
172     QWidget::resizeEvent(re);
173 }
174
175 void RightPaneWidget::saveSettings(QSettings *settings)
176 {
177     settings->setValue("RightPane/Visible", isShown());
178     settings->setValue("RightPane/Width", m_width);
179 }
180
181 void RightPaneWidget::readSettings(QSettings *settings)
182 {
183     if (settings->contains("RightPane/Visible")) {
184         setShown(settings->value("RightPane/Visible").toBool());
185     } else {
186         setShown(false); //TODO set to false
187     }
188
189     if (settings->contains("RightPane/Width")) {
190         m_width = settings->value("RightPane/Width").toInt();
191         if (!m_width)
192             m_width = 500;
193     } else {
194         m_width = 500; //pixel
195     }
196     // Apply
197     if (RightPanePlaceHolder::m_current) {
198         RightPanePlaceHolder::m_current->applyStoredSize(m_width);
199     }
200 }
201
202 void RightPaneWidget::setShown(bool b)
203 {
204     if (RightPanePlaceHolder::m_current)
205         RightPanePlaceHolder::m_current->setVisible(b);
206     m_shown = b;
207 }
208
209 bool RightPaneWidget::isShown()
210 {
211     return m_shown;
212 }
213
214 void RightPaneWidget::clearWidget()
215 {
216     if (m_widget) {
217         m_widget->hide();
218         m_widget->setParent(0);
219         m_widget = 0;
220     }
221 }