File indexing completed on 2024-05-12 16:39:51

0001 /* This file is part of the KDE project
0002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
0003    Copyright (C) 2005,2010 Jarosław Staniek <staniek@kde.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include <QCheckBox>
0022 #include <QVBoxLayout>
0023 #include <QGridLayout>
0024 #include <QPushButton>
0025 #include <QDialogButtonBox>
0026 #include <QDebug>
0027 
0028 #include <KLocalizedString>
0029 
0030 #include <KexiIcon.h>
0031 
0032 #include "form.h"
0033 #include "WidgetTreeWidget.h"
0034 #include "tabstopdialog.h"
0035 
0036 using namespace KFormDesigner;
0037 
0038 //////////////////////////////////////////////////////////////////////////////////
0039 //////////  The Tab Stop Dialog to edit tab order  ///////////////////////////////
0040 //////////////////////////////////////////////////////////////////////////////////
0041 
0042 class Q_DECL_HIDDEN TabStopDialog::Private
0043 {
0044 public:
0045     Private();
0046     ~Private();
0047 
0048     WidgetTreeWidget *widgetTree;
0049     QPushButton *btnUp, *btnDown;
0050     QCheckBox *check;
0051 };
0052 
0053 TabStopDialog::Private::Private()
0054 {
0055 
0056 }
0057 
0058 TabStopDialog::Private::~Private()
0059 {
0060 
0061 }
0062 
0063 TabStopDialog::TabStopDialog(QWidget *parent)    : QDialog(parent), d(new Private())
0064 {
0065     setObjectName("tabstop_dialog");
0066     setModal(true);
0067     setWindowTitle(xi18nc("@title:window", "Edit Tab Order"));
0068 
0069     QVBoxLayout *mainLayout = new QVBoxLayout;
0070     setLayout(mainLayout);
0071 
0072     QGridLayout *l = new QGridLayout;
0073     mainLayout->addLayout(l);
0074     d->widgetTree = new WidgetTreeWidget(this,
0075         WidgetTreeWidget::DisableSelection | WidgetTreeWidget::DisableContextMenu);
0076     d->widgetTree->setObjectName("tabstops:widgetTree");
0077     d->widgetTree->setDragEnabled(true);
0078     d->widgetTree->setDropIndicatorShown(true);
0079     d->widgetTree->setDragDropMode(QAbstractItemView::InternalMove);
0080     d->widgetTree->setAcceptDrops(true);
0081     l->addWidget(d->widgetTree, 0, 0);
0082 
0083     d->widgetTree->setForm(0);
0084     connect(d->widgetTree, SIGNAL(itemSelectionChanged()), this, SLOT(slotSelectionChanged()));
0085 
0086 //! @todo KEXI3 TODO connect(d->widgetTree, SIGNAL(moved(QListViewItem*,QListViewItem*,QListViewItem*)), this, SLOT(updateButtons(QListViewItem*)));
0087 
0088     QVBoxLayout *vbox = new QVBoxLayout();
0089     l->addLayout(vbox, 0, 1);
0090     d->btnUp = new QPushButton(koIcon("arrow-up"), xi18n("Move Up"), this);
0091     d->btnUp->setToolTip(xi18n("Move widget up"));
0092     vbox->addWidget(d->btnUp);
0093     connect(d->btnUp, SIGNAL(clicked()), this, SLOT(moveItemUp()));
0094 
0095     d->btnDown = new QPushButton(koIcon("arrow-down"), xi18n("Move Down"), this);
0096     d->btnDown->setToolTip(xi18n("Move widget down"));
0097     vbox->addWidget(d->btnDown);
0098     connect(d->btnDown, SIGNAL(clicked()), this, SLOT(moveItemDown()));
0099     vbox->addStretch();
0100 
0101     d->check = new QCheckBox(xi18n("Handle tab order automatically"), this);
0102     d->check->setObjectName("tabstops_check");
0103     connect(d->check, SIGNAL(toggled(bool)), this, SLOT(slotRadioClicked(bool)));
0104     l->addWidget(d->check, 1, 0, 1, 2);
0105 
0106     // buttons
0107     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
0108     buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
0109     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0110     okButton->setDefault(true);
0111     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0112     connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
0113     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
0114     mainLayout->addWidget(buttonBox);
0115 
0116     updateGeometry();
0117     resize(QSize(500 + d->btnUp->width(), qMax(400, d->widgetTree->height())));
0118 }
0119 
0120 TabStopDialog::~TabStopDialog()
0121 {
0122     delete d;
0123 }
0124 
0125 int TabStopDialog::exec(Form *form)
0126 {
0127     d->widgetTree->clear();
0128     d->widgetTree->setForm(form);
0129 
0130     if (form->autoTabStops())
0131         form->autoAssignTabStops();
0132     form->updateTabStopsOrder();
0133     if (!form->tabStops()->isEmpty()) {
0134         ObjectTreeList::ConstIterator it(form->tabStops()->constBegin());
0135         it+=(form->tabStops()->count()-1);
0136         for (;it!=form->tabStops()->constEnd(); --it) {
0137             new WidgetTreeWidgetItem(d->widgetTree, *it);
0138         }
0139     }
0140     d->check->setChecked(form->autoTabStops());
0141 
0142     if (d->widgetTree->invisibleRootItem()->childCount() > 0) {
0143         QTreeWidgetItem *firstItem = d->widgetTree->invisibleRootItem()->child(0);
0144         d->widgetTree->setCurrentItem(firstItem);
0145         firstItem->setSelected(true);
0146     }
0147 
0148     if (QDialog::Rejected == QDialog::exec())
0149         return QDialog::Rejected;
0150 
0151     //accepted
0152     form->setAutoTabStops(d->check->isChecked());
0153     if (form->autoTabStops()) {
0154         form->autoAssignTabStops();
0155         return QDialog::Accepted;
0156     }
0157 
0158     //add items to the order list
0159     form->tabStops()->clear();
0160     QTreeWidgetItemIterator it(d->widgetTree);
0161     while (*it) {
0162         ObjectTreeItem *tree = static_cast<WidgetTreeWidgetItem*>(*it)->data();
0163         if (tree)
0164             form->tabStops()->append(tree);
0165     }
0166     return QDialog::Accepted;
0167 }
0168 
0169 void TabStopDialog::moveItemUp()
0170 {
0171     QTreeWidgetItem *selected = d->widgetTree->selectedItem();
0172     if (!selected)
0173         return;
0174     // we assume there is flat list
0175     QTreeWidgetItem *root = d->widgetTree->invisibleRootItem();
0176     const int selectedIndex = root->indexOfChild(selected);
0177     if (selectedIndex < 1)
0178         return; // no place to move
0179     root->takeChild(selectedIndex);
0180     root->insertChild(selectedIndex - 1, selected);
0181     updateButtons(selected);
0182 }
0183 
0184 void TabStopDialog::moveItemDown()
0185 {
0186     QTreeWidgetItem *selected = d->widgetTree->selectedItem();
0187     if (!selected)
0188         return;
0189     // we assume there is flat list
0190     QTreeWidgetItem *root = d->widgetTree->invisibleRootItem();
0191     const int selectedIndex = root->indexOfChild(selected);
0192     if (selectedIndex >= (root->childCount() - 1))
0193         return; // no place to move
0194     root->takeChild(selectedIndex);
0195     root->insertChild(selectedIndex + 1, selected);
0196     updateButtons(selected);
0197 }
0198 
0199 void TabStopDialog::updateButtons(QTreeWidgetItem *item)
0200 {
0201     QTreeWidgetItem *root = d->widgetTree->invisibleRootItem();
0202     d->btnUp->setEnabled(item && (root->indexOfChild(item) > 0 && d->widgetTree->isEnabled()
0203                                  /*&& (item->itemAbove()->parent() == item->parent()))*/));
0204     d->btnDown->setEnabled(item && root->indexOfChild(item) < (root->childCount() - 1) && d->widgetTree->isEnabled());
0205 }
0206 
0207 void TabStopDialog::slotSelectionChanged()
0208 {
0209     updateButtons(d->widgetTree->selectedItem());
0210 }
0211 
0212 void TabStopDialog::slotRadioClicked(bool isOn)
0213 {
0214     d->widgetTree->setEnabled(!isOn);
0215     updateButtons(d->widgetTree->selectedItem());
0216 }
0217 
0218 bool TabStopDialog::autoTabStops() const
0219 {
0220     return d->check->isChecked();
0221 }
0222