File indexing completed on 2024-05-12 16:36:09
0001 /* This file is part of the KDE project 0002 Copyright 1999-2006 The KSpread Team <calligra-devel@kde.org> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This library is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LIB. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "RegionSelector.h" 0021 0022 // Sheets 0023 #include "FormulaEditorHighlighter.h" 0024 #include "Selection.h" 0025 #include "SheetsDebug.h" 0026 0027 // Calligra 0028 #include <KoIcon.h> 0029 #include <KoDialog.h> 0030 0031 // KF5 0032 #include <KLocalizedString> 0033 #include <ktextedit.h> 0034 0035 // Qt 0036 #include <QEvent> 0037 #include <QHBoxLayout> 0038 #include <QToolButton> 0039 0040 using namespace Calligra::Sheets; 0041 0042 class Q_DECL_HIDDEN RegionSelector::Private 0043 { 0044 public: 0045 Selection* selection; 0046 QDialog* parentDialog; 0047 KoDialog* dialog; 0048 KTextEdit* textEdit; 0049 QToolButton* button; 0050 FormulaEditorHighlighter* highlighter; 0051 DisplayMode displayMode; 0052 SelectionMode selectionMode; 0053 static RegionSelector* s_focussedSelector; 0054 }; 0055 0056 RegionSelector* RegionSelector::Private::s_focussedSelector = 0; 0057 0058 RegionSelector::RegionSelector(QWidget* parent) 0059 : QWidget(parent), 0060 d(new Private) 0061 { 0062 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum); 0063 0064 d->displayMode = Widget; 0065 d->parentDialog = 0; 0066 d->selection = 0; 0067 d->dialog = 0; 0068 d->button = new QToolButton(this); 0069 d->button->setCheckable(true); 0070 d->button->setIcon(koIcon("selection")); 0071 d->highlighter = 0; 0072 d->textEdit = new KTextEdit(this); 0073 d->textEdit->setLineWrapMode(QTextEdit::NoWrap); 0074 d->textEdit->setWordWrapMode(QTextOption::NoWrap); 0075 d->textEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum); 0076 d->textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 0077 d->textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 0078 d->textEdit->setFixedHeight(d->button->height() - 2*d->textEdit->frameWidth()); // FIXME 0079 d->textEdit->setTabChangesFocus(true); 0080 0081 QHBoxLayout* layout = new QHBoxLayout(this); 0082 layout->setMargin(0); 0083 layout->setSpacing(2); 0084 layout->addWidget(d->textEdit); 0085 layout->addWidget(d->button); 0086 0087 d->button->installEventFilter(this); 0088 d->textEdit->installEventFilter(this); 0089 connect(d->button, SIGNAL(toggled(bool)), 0090 this, SLOT(switchDisplayMode(bool))); 0091 } 0092 0093 RegionSelector::~RegionSelector() 0094 { 0095 d->selection->endReferenceSelection(); 0096 d->selection->setSelectionMode(Selection::MultipleCells); 0097 delete d; 0098 } 0099 0100 void RegionSelector::setSelectionMode(SelectionMode mode) 0101 { 0102 d->selectionMode = mode; 0103 // TODO adjust selection 0104 } 0105 0106 void RegionSelector::setSelection(Selection* selection) 0107 { 0108 d->selection = selection; 0109 d->highlighter = new FormulaEditorHighlighter(d->textEdit, d->selection); 0110 connect(d->selection, SIGNAL(changed(Region)), this, SLOT(choiceChanged())); 0111 } 0112 0113 void RegionSelector::setDialog(QDialog* dialog) 0114 { 0115 d->parentDialog = dialog; 0116 } 0117 0118 KTextEdit* RegionSelector::textEdit() const 0119 { 0120 return d->textEdit; 0121 } 0122 0123 bool RegionSelector::eventFilter(QObject* object, QEvent* event) 0124 { 0125 if (event->type() == QEvent::Close) { 0126 if (object == d->dialog && d->button->isChecked()) { 0127 // TODO Stefan: handle as button click 0128 // d->button->toggle(); 0129 event->ignore(); 0130 return true; // eat it 0131 } 0132 } else if (event->type() == QEvent::FocusIn) { 0133 Private::s_focussedSelector = this; 0134 d->selection->startReferenceSelection(); 0135 if (d->selectionMode == SingleCell) { 0136 d->selection->setSelectionMode(Selection::SingleCell); 0137 } else { 0138 d->selection->setSelectionMode(Selection::MultipleCells); 0139 } 0140 // TODO Stefan: initialize choice 0141 } 0142 return QObject::eventFilter(object, event); 0143 } 0144 0145 void RegionSelector::switchDisplayMode(bool state) 0146 { 0147 Q_UNUSED(state) 0148 debugSheets ; 0149 0150 if (d->displayMode == Widget) { 0151 d->displayMode = Dialog; 0152 0153 d->dialog = new KoDialog(d->parentDialog->parentWidget(), Qt::Tool); 0154 d->dialog->resize(d->parentDialog->width(), 20); 0155 d->dialog->move(d->parentDialog->pos()); 0156 d->dialog->setButtons(0); 0157 d->dialog->setModal(false); 0158 0159 if (d->selectionMode == SingleCell) { 0160 d->dialog->setCaption(i18n("Select Single Cell")); 0161 } else { // if ( d->selectionMode == MultipleCells ) 0162 d->dialog->setCaption(i18n("Select Multiple Cells")); 0163 } 0164 0165 QWidget* widget = new QWidget(d->dialog); 0166 QHBoxLayout* layout = new QHBoxLayout(widget); 0167 layout->setMargin(0); 0168 layout->setSpacing(0); 0169 layout->addWidget(d->textEdit); 0170 layout->addWidget(d->button); 0171 0172 d->dialog->setMainWidget(widget); 0173 d->dialog->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); 0174 d->dialog->installEventFilter(this); 0175 d->dialog->show(); 0176 d->parentDialog->hide(); 0177 } else { 0178 d->displayMode = Widget; 0179 0180 layout()->addWidget(d->textEdit); 0181 layout()->addWidget(d->button); 0182 0183 d->parentDialog->move(d->dialog->pos()); 0184 d->parentDialog->show(); 0185 delete d->dialog; 0186 d->dialog = 0; 0187 } 0188 } 0189 0190 void RegionSelector::choiceChanged() 0191 { 0192 if (Private::s_focussedSelector != this) 0193 return; 0194 0195 if (d->selection->isValid()) { 0196 QString area = d->selection->name(); 0197 d->textEdit->setPlainText(area); 0198 } 0199 }