1/*
2 Copyright (c) 2012 Montel Laurent <montel@kde.org>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18
19*/
20
21#include "inserttabledialog.h"
22
23#include <KLocalizedString>
24#include <KComboBox>
25#include <KDebug>
26#include <KSeparator>
27
28#include <QSpinBox>
29#include <QFormLayout>
30
31using namespace KPIMTextEdit;
32
33class InsertTableWidget::InsertTableWidgetPrivate
34{
35 public:
36 InsertTableWidgetPrivate( InsertTableWidget *qq )
37 :q( qq )
38 {
39 mRows = new QSpinBox;
40 mRows->setMinimum( 1 );
41 mRows->setValue( 2 );
42
43 mColumns = new QSpinBox;
44 mColumns->setMinimum( 1 );
45 mColumns->setValue( 2 );
46
47 mBorder = new QSpinBox;
48 mBorder->setMinimum( 0 );
49 mBorder->setValue( 1 );
50 mBorder->setSuffix( i18n( " px" ) );
51
52 QFormLayout *formLayout = new QFormLayout;
53 formLayout->addRow( i18n( "Rows:" ), mRows );
54 formLayout->addRow( i18n( "Columns:" ), mColumns );
55 formLayout->addRow( i18n( "Border:" ), mBorder );
56
57 QHBoxLayout *lay = new QHBoxLayout;
58 mTypeOfLength = new KComboBox;
59 q->connect( mTypeOfLength, SIGNAL(activated(int)),q,SLOT(slotTypeOfLengthChanged(int)) );
60 // xgettext: no-c-format
61 mTypeOfLength->addItem( i18n( "% of windows" ), QTextLength::PercentageLength );
62 mTypeOfLength->addItem( i18n( "pixels" ), QTextLength::FixedLength );
63 mLength = new QSpinBox;
64 mLength->setMinimum( 1 );
65 mLength->setMaximum( 100 );
66 mLength->setValue( 100 );
67 lay->addWidget( mLength );
68 lay->addWidget( mTypeOfLength );
69
70 formLayout->addRow( i18n( "Width:" ), lay );
71 q->setLayout( formLayout );
72 }
73
74 QSpinBox *mColumns;
75 QSpinBox *mRows;
76 QSpinBox *mBorder;
77 QSpinBox *mLength;
78 KComboBox *mTypeOfLength;
79
80 InsertTableWidget *q;
81};
82
83InsertTableWidget::InsertTableWidget( QWidget *parent )
84 : QWidget( parent ), d( new InsertTableWidgetPrivate( this ) )
85{
86}
87
88InsertTableWidget::~InsertTableWidget()
89{
90 delete d;
91}
92
93void InsertTableWidget::slotTypeOfLengthChanged( int index )
94{
95 switch ( index ) {
96 case 0:
97 d->mLength->setMaximum( 100 );
98 d->mLength->setValue( qMin( d->mLength->value(), 100 ) );
99 break;
100 case 1:
101 d->mLength->setMaximum( 9999 );
102 break;
103 default:
104 kDebug() << " index not defined " << index;
105 break;
106 }
107}
108
109QTextLength::Type InsertTableWidget::typeOfLength() const
110{
111 return
112 ( QTextLength::Type )d->mTypeOfLength->itemData(
113 d->mTypeOfLength->currentIndex() ).toInt();
114}
115
116void InsertTableWidget::setTypeOfLength( QTextLength::Type type )
117{
118 const int index = d->mTypeOfLength->findData( QVariant( type ) );
119 d->mTypeOfLength->setCurrentIndex( index );
120 slotTypeOfLengthChanged( index );
121}
122
123int InsertTableWidget::length() const
124{
125 return d->mLength->value();
126}
127
128void InsertTableWidget::setLength( int val )
129{
130 d->mLength->setValue(val);
131}
132
133void InsertTableWidget::setColumns( int col )
134{
135 d->mColumns->setValue( col );
136}
137
138void InsertTableWidget::setRows( int rows )
139{
140 d->mRows->setValue( rows );
141}
142
143void InsertTableWidget::setBorder( int border )
144{
145 d->mBorder->setValue( border );
146}
147
148int InsertTableWidget::columns() const
149{
150 return d->mColumns->value();
151}
152
153int InsertTableWidget::rows() const
154{
155 return d->mRows->value();
156}
157
158int InsertTableWidget::border() const
159{
160 return d->mBorder->value();
161}
162
163class InsertTableDialog::InsertTableDialogPrivate
164{
165 public:
166 InsertTableDialogPrivate( InsertTableDialog *qq )
167 : q( qq )
168 {
169 q->setCaption( i18n( "Insert Table" ) );
170 q->setButtons( Ok|Cancel );
171 q->setButtonText( KDialog::Ok, i18n( "Insert" ) );
172 QWidget *page = new QWidget;
173 QVBoxLayout *lay = new QVBoxLayout;
174 page->setLayout(lay);
175 insertTableWidget = new InsertTableWidget;
176 lay->addWidget(insertTableWidget);
177 KSeparator *sep = new KSeparator;
178 lay->addWidget( sep );
179
180 q->setMainWidget( page );
181 }
182
183 InsertTableWidget *insertTableWidget;
184 InsertTableDialog *q;
185};
186
187InsertTableDialog::InsertTableDialog( QWidget *parent )
188 : KDialog( parent ), d( new InsertTableDialogPrivate( this ) )
189{
190}
191
192InsertTableDialog::~InsertTableDialog()
193{
194 delete d;
195}
196
197int InsertTableDialog::columns() const
198{
199 return d->insertTableWidget->columns();
200}
201
202int InsertTableDialog::rows() const
203{
204 return d->insertTableWidget->rows();
205}
206
207int InsertTableDialog::border() const
208{
209 return d->insertTableWidget->border();
210}
211
212QTextLength::Type InsertTableDialog::typeOfLength() const
213{
214 return d->insertTableWidget->typeOfLength();
215}
216
217int InsertTableDialog::length() const
218{
219 return d->insertTableWidget->length();
220}
221
222void InsertTableDialog::setColumns( int col )
223{
224 d->insertTableWidget->setColumns( col );
225}
226
227void InsertTableDialog::setRows( int rows )
228{
229 d->insertTableWidget->setRows( rows );
230}
231
232void InsertTableDialog::setBorder( int border )
233{
234 d->insertTableWidget->setBorder( border );
235}
236
237void InsertTableDialog::setLength( int val )
238{
239 d->insertTableWidget->setLength( val );
240}
241
242void InsertTableDialog::setTypeOfLength( QTextLength::Type type )
243{
244 d->insertTableWidget->setTypeOfLength( type );
245}
246