1 /* This file is part of the KDE project
2    Copyright 1999-2006 The KSpread Team <calligra-devel@kde.org>
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public 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
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17    Boston, MA 02110-1301, USA.
18 */
19 
20 #include "RegionSelector.h"
21 
22 // Sheets
23 #include "FormulaEditorHighlighter.h"
24 #include "Selection.h"
25 #include "SheetsDebug.h"
26 
27 // Calligra
28 #include <KoIcon.h>
29 #include <KoDialog.h>
30 
31 // KF5
32 #include <KLocalizedString>
33 #include <ktextedit.h>
34 
35 // Qt
36 #include <QEvent>
37 #include <QHBoxLayout>
38 #include <QToolButton>
39 
40 using namespace Calligra::Sheets;
41 
42 class Q_DECL_HIDDEN RegionSelector::Private
43 {
44 public:
45     Selection* selection;
46     QDialog* parentDialog;
47     KoDialog* dialog;
48     KTextEdit* textEdit;
49     QToolButton* button;
50     FormulaEditorHighlighter* highlighter;
51     DisplayMode displayMode;
52     SelectionMode selectionMode;
53     static RegionSelector* s_focussedSelector;
54 };
55 
56 RegionSelector* RegionSelector::Private::s_focussedSelector = 0;
57 
RegionSelector(QWidget * parent)58 RegionSelector::RegionSelector(QWidget* parent)
59         : QWidget(parent),
60         d(new Private)
61 {
62     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
63 
64     d->displayMode = Widget;
65     d->parentDialog = 0;
66     d->selection = 0;
67     d->dialog = 0;
68     d->button = new QToolButton(this);
69     d->button->setCheckable(true);
70     d->button->setIcon(koIcon("selection"));
71     d->highlighter = 0;
72     d->textEdit = new KTextEdit(this);
73     d->textEdit->setLineWrapMode(QTextEdit::NoWrap);
74     d->textEdit->setWordWrapMode(QTextOption::NoWrap);
75     d->textEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
76     d->textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
77     d->textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
78     d->textEdit->setFixedHeight(d->button->height() - 2*d->textEdit->frameWidth());   // FIXME
79     d->textEdit->setTabChangesFocus(true);
80 
81     QHBoxLayout* layout = new QHBoxLayout(this);
82     layout->setMargin(0);
83     layout->setSpacing(2);
84     layout->addWidget(d->textEdit);
85     layout->addWidget(d->button);
86 
87     d->button->installEventFilter(this);
88     d->textEdit->installEventFilter(this);
89     connect(d->button, SIGNAL(toggled(bool)),
90             this, SLOT(switchDisplayMode(bool)));
91 }
92 
~RegionSelector()93 RegionSelector::~RegionSelector()
94 {
95     d->selection->endReferenceSelection();
96     d->selection->setSelectionMode(Selection::MultipleCells);
97     delete d;
98 }
99 
setSelectionMode(SelectionMode mode)100 void RegionSelector::setSelectionMode(SelectionMode mode)
101 {
102     d->selectionMode = mode;
103     // TODO adjust selection
104 }
105 
setSelection(Selection * selection)106 void RegionSelector::setSelection(Selection* selection)
107 {
108     d->selection = selection;
109     d->highlighter = new FormulaEditorHighlighter(d->textEdit, d->selection);
110     connect(d->selection, SIGNAL(changed(Region)), this, SLOT(choiceChanged()));
111 }
112 
setDialog(QDialog * dialog)113 void RegionSelector::setDialog(QDialog* dialog)
114 {
115     d->parentDialog = dialog;
116 }
117 
textEdit() const118 KTextEdit* RegionSelector::textEdit() const
119 {
120     return d->textEdit;
121 }
122 
eventFilter(QObject * object,QEvent * event)123 bool RegionSelector::eventFilter(QObject* object, QEvent* event)
124 {
125     if (event->type() == QEvent::Close) {
126         if (object == d->dialog  && d->button->isChecked()) {
127             // TODO Stefan: handle as button click
128 //       d->button->toggle();
129             event->ignore();
130             return true; // eat it
131         }
132     } else if (event->type() == QEvent::FocusIn) {
133         Private::s_focussedSelector = this;
134         d->selection->startReferenceSelection();
135         if (d->selectionMode == SingleCell) {
136             d->selection->setSelectionMode(Selection::SingleCell);
137         } else {
138             d->selection->setSelectionMode(Selection::MultipleCells);
139         }
140         // TODO Stefan: initialize choice
141     }
142     return QObject::eventFilter(object, event);
143 }
144 
switchDisplayMode(bool state)145 void RegionSelector::switchDisplayMode(bool state)
146 {
147     Q_UNUSED(state)
148     debugSheets ;
149 
150     if (d->displayMode == Widget) {
151         d->displayMode = Dialog;
152 
153         d->dialog = new KoDialog(d->parentDialog->parentWidget(), Qt::Tool);
154         d->dialog->resize(d->parentDialog->width(), 20);
155         d->dialog->move(d->parentDialog->pos());
156         d->dialog->setButtons(0);
157         d->dialog->setModal(false);
158 
159         if (d->selectionMode == SingleCell) {
160             d->dialog->setCaption(i18n("Select Single Cell"));
161         } else { // if ( d->selectionMode == MultipleCells )
162             d->dialog->setCaption(i18n("Select Multiple Cells"));
163         }
164 
165         QWidget* widget = new QWidget(d->dialog);
166         QHBoxLayout* layout = new QHBoxLayout(widget);
167         layout->setMargin(0);
168         layout->setSpacing(0);
169         layout->addWidget(d->textEdit);
170         layout->addWidget(d->button);
171 
172         d->dialog->setMainWidget(widget);
173         d->dialog->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
174         d->dialog->installEventFilter(this);
175         d->dialog->show();
176         d->parentDialog->hide();
177     } else {
178         d->displayMode = Widget;
179 
180         layout()->addWidget(d->textEdit);
181         layout()->addWidget(d->button);
182 
183         d->parentDialog->move(d->dialog->pos());
184         d->parentDialog->show();
185         delete d->dialog;
186         d->dialog = 0;
187     }
188 }
189 
choiceChanged()190 void RegionSelector::choiceChanged()
191 {
192     if (Private::s_focussedSelector != this)
193         return;
194 
195     if (d->selection->isValid()) {
196         QString area = d->selection->name();
197         d->textEdit->setPlainText(area);
198     }
199 }
200