~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to tools/designer/src/components/formeditor/formwindow_widgetstack.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the designer application of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <QtDesigner/QtDesigner>
 
30
#include "formwindow_widgetstack.h"
 
31
 
 
32
#include <QtGui/QWidget>
 
33
#include <QtGui/qevent.h>
 
34
#include <QtGui/QAction>
 
35
 
 
36
#include <QtCore/qdebug.h>
 
37
 
 
38
using namespace qdesigner_internal;
 
39
 
 
40
FormWindowWidgetStack::FormWindowWidgetStack(QWidget *parent)
 
41
    : QWidget(parent),
 
42
      m_current_index(-1)
 
43
{
 
44
}
 
45
 
 
46
FormWindowWidgetStack::~FormWindowWidgetStack()
 
47
{
 
48
}
 
49
 
 
50
int FormWindowWidgetStack::count() const
 
51
{
 
52
    return m_tools.count();
 
53
}
 
54
 
 
55
QDesignerFormWindowToolInterface *FormWindowWidgetStack::currentTool() const
 
56
{
 
57
    return tool(m_current_index);
 
58
}
 
59
 
 
60
void FormWindowWidgetStack::setCurrentTool(int index)
 
61
{
 
62
    if (index < 0 || index >= count()) {
 
63
        qWarning("FormWindowWidgetStack::setCurrentTool(): invalid index: %d", index);
 
64
        return;
 
65
    }
 
66
 
 
67
    if (index == m_current_index)
 
68
        return;
 
69
 
 
70
    if (m_current_index != -1)
 
71
        m_tools.at(m_current_index)->deactivated();
 
72
 
 
73
    m_current_index = index;
 
74
 
 
75
    QDesignerFormWindowToolInterface *tool = m_tools.at(m_current_index);
 
76
    tool->activated();
 
77
    QWidget *w = tool->editor();
 
78
    if (w != 0) {
 
79
        if (w->rect() != rect())
 
80
            w->setGeometry(rect());
 
81
        w->raise();
 
82
    }
 
83
 
 
84
    emit currentToolChanged(index);
 
85
}
 
86
 
 
87
void FormWindowWidgetStack::setSenderAsCurrentTool()
 
88
{
 
89
    QDesignerFormWindowToolInterface *tool = 0;
 
90
    QAction *action = qobject_cast<QAction*>(sender());
 
91
    if (action == 0) {
 
92
        qWarning("FormWindowWidgetStack::setSenderAsCurrentTool(): sender is not a QAction");
 
93
        return;
 
94
    }
 
95
 
 
96
    foreach (QDesignerFormWindowToolInterface *t, m_tools) {
 
97
        if (action == t->action()) {
 
98
            tool = t;
 
99
            break;
 
100
        }
 
101
    }
 
102
 
 
103
    if (tool == 0) {
 
104
        qWarning("FormWindowWidgetStack::setSenderAsCurrentTool(): unknown tool");
 
105
        return;
 
106
    }
 
107
 
 
108
    setCurrentTool(tool);
 
109
}
 
110
 
 
111
int FormWindowWidgetStack::indexOf(QDesignerFormWindowToolInterface *tool) const
 
112
{
 
113
    for (int i = 0; i < m_tools.size(); ++i) {
 
114
        if (m_tools.at(i) == tool)
 
115
            return i;
 
116
    }
 
117
 
 
118
    return -1;
 
119
}
 
120
 
 
121
void FormWindowWidgetStack::setCurrentTool(QDesignerFormWindowToolInterface *tool)
 
122
{
 
123
    int index = indexOf(tool);
 
124
    if (index == -1) {
 
125
        qWarning("FormWindowWidgetStack::setCurrentTool(): unknown tool");
 
126
        return;
 
127
    }
 
128
 
 
129
    setCurrentTool(index);
 
130
}
 
131
 
 
132
void FormWindowWidgetStack::addTool(QDesignerFormWindowToolInterface *tool)
 
133
{
 
134
    QWidget *w = tool->editor();
 
135
    if (w != 0)
 
136
        w->setParent(this);
 
137
 
 
138
    m_tools.append(tool);
 
139
 
 
140
    connect(tool->action(), SIGNAL(triggered()), this, SLOT(setSenderAsCurrentTool()));
 
141
}
 
142
 
 
143
void FormWindowWidgetStack::resizeEvent(QResizeEvent *event)
 
144
{
 
145
    QWidget::resizeEvent(event);
 
146
 
 
147
    QRect r = QRect(0, 0, event->size().width(), event->size().height());
 
148
 
 
149
    // We always resize the widget tool
 
150
    QDesignerFormWindowToolInterface *widget_tool = tool(0);
 
151
    if (widget_tool != 0 && widget_tool->editor() != 0)
 
152
        widget_tool->editor()->setGeometry(r);
 
153
 
 
154
    QDesignerFormWindowToolInterface *cur_tool = currentTool();
 
155
    if (cur_tool == widget_tool)
 
156
        return;
 
157
 
 
158
    if (cur_tool != 0 && cur_tool->editor() != 0)
 
159
        cur_tool->editor()->setGeometry(r);
 
160
}
 
161
 
 
162
QDesignerFormWindowToolInterface *FormWindowWidgetStack::tool(int index) const
 
163
{
 
164
    if (index < 0 || index >= count())
 
165
        return 0;
 
166
 
 
167
    return m_tools.at(index);
 
168
}
 
169
 
 
170
int FormWindowWidgetStack::currentIndex() const
 
171
{
 
172
    return m_current_index;
 
173
}