File: LocationComboBox.cpp

package info (click to toggle)
calligra 1%3A2.9.11%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 189,332 kB
  • sloc: cpp: 919,806; xml: 27,759; ansic: 10,472; python: 8,190; perl: 2,724; yacc: 2,557; sh: 1,675; lex: 1,431; java: 1,304; sql: 903; ruby: 734; makefile: 48
file content (277 lines) | stat: -rw-r--r-- 8,415 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* This file is part of the KDE project
   Copyright 1999-2006 The KSpread Team <calligra-devel@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "LocationComboBox.h"

// KSpread
#include "CellStorage.h"
#include "Map.h"
#include "NamedAreaManager.h"
#include "Selection.h"
#include "Sheet.h"

#include "commands/NamedAreaCommand.h"

// Calligra

// KDE
#include <kdebug.h>
#include <kglobalsettings.h>

// Qt
#include <QKeyEvent>
#include <QLineEdit>

using namespace Calligra::Sheets;

LocationComboBox::LocationComboBox(QWidget *_parent)
        : KComboBox(true, _parent)
        , m_selection(0)
{
    setCompletionObject(&completionList, true);
    setCompletionMode(KGlobalSettings::CompletionAuto);

    connect(this, SIGNAL(activated(QString)),
            this, SLOT(slotActivateItem()));
}

void LocationComboBox::setSelection(Selection *selection)
{
    if (m_selection == selection) return;

    completionList.clear();
    clear();
    if (m_selection) {
        if (m_selection->activeSheet()) {
            Map *const oldMap = m_selection->activeSheet()->map();
            disconnect(oldMap->namedAreaManager(), SIGNAL(namedAreaAdded(QString)), this, SLOT(slotAddAreaName(QString)));
            disconnect(oldMap->namedAreaManager(), SIGNAL(namedAreaRemoved(QString)), this, SLOT(slotRemoveAreaName(QString)));
        }
        disconnect(m_selection, SIGNAL(activeSheetChanged(Sheet*)), this, SLOT(slotActiveSheetChanged(Sheet*)));
        disconnect(m_selection, SIGNAL(changed(Region)), this, SLOT(slotSelectionChanged()));
    }

    m_selection = selection;

    if (m_selection) {
        insertItem(0, QString());
        updateAddress();
        Sheet* sheet = m_selection->activeSheet();
        if (sheet) {
            slotActiveSheetChanged(sheet);
        } else {
            connect(m_selection, SIGNAL(activeSheetChanged(Sheet*)), this, SLOT(slotActiveSheetChanged(Sheet*)));
        }
        connect(m_selection, SIGNAL(changed(Region)), this, SLOT(slotSelectionChanged()));
    }
}

void LocationComboBox::slotActiveSheetChanged(Sheet *sheet)
{
    if (!sheet) return;
    disconnect(this, SLOT(slotActiveSheetChanged(Sheet*)));

    Map *const map = sheet->map();
    const QList<QString> areaNames = map->namedAreaManager()->areaNames();
    for (int i = 0; i < areaNames.count(); ++i)
        slotAddAreaName(areaNames[i]);

    connect(map->namedAreaManager(), SIGNAL(namedAreaAdded(QString)), this, SLOT(slotAddAreaName(QString)));
    connect(map->namedAreaManager(), SIGNAL(namedAreaRemoved(QString)), this, SLOT(slotRemoveAreaName(QString)));
}

void LocationComboBox::updateAddress()
{
    if (!m_selection) return;

    QString address;
    Selection *const selection = m_selection;
    Sheet *const sheet = m_selection->activeSheet();
    if (sheet) {
        const QList< QPair<QRectF, QString> > names = sheet->cellStorage()->namedAreas(*selection);
        {
            QRect range;
            if (selection->isSingular()) range = QRect(selection->marker(), QSize(1, 1));
            else range = selection->lastRange();
            for (int i = 0; i < names.size(); i++) {
                if (names[i].first.toRect() == range) {
                    address = names[i].second;
                }
            }
        }
    }
    if (sheet && sheet->getLcMode()) {
        if (selection->isSingular()) {
            address = 'L' + QString::number(selection->marker().y()) +
            'C' + QString::number(selection->marker().x());
        } else {
            const QRect lastRange = selection->lastRange();
            address = QString::number(lastRange.height()) + "Lx";
            address += QString::number(lastRange.width()) + 'C';
        }
    } else {
        address = selection->name();
    }
    setItemText(0, address);
    setCurrentItem(0);
    lineEdit()->setText(address);
}

void LocationComboBox::slotAddAreaName(const QString &_name)
{
    insertItem(count(), _name);
    addCompletionItem(_name);
}

void LocationComboBox::slotRemoveAreaName(const QString &_name)
{
    for (int i = 0; i < count(); i++) {
        if (itemText(i) == _name) {
            removeItem(i);
            break;
        }
    }
    removeCompletionItem(_name);
}

void LocationComboBox::addCompletionItem(const QString &_item)
{
    if (completionList.items().contains(_item) == 0) {
        completionList.addItem(_item);
        kDebug(36005) << _item;
    }
}

void LocationComboBox::removeCompletionItem(const QString &_item)
{
    completionList.removeItem(_item);
}

void LocationComboBox::slotActivateItem()
{
    if (!m_selection) return;

    if (activateItem()) {
        m_selection->scrollToCursor();
    }
}

bool LocationComboBox::activateItem()
{
    if (!m_selection) return false;

    Selection *const selection = m_selection;

    // Set the focus back on the canvas.
    parentWidget()->setFocus();

    const QString text = lineEdit()->text();
    // check whether an already existing named area was entered
    Region region = selection->activeSheet()->map()->namedAreaManager()->namedArea(text);
    if (region.isValid()) {
        // TODO Stefan: Merge the sheet change into Selection.
        if (region.firstSheet() != selection->activeSheet()) {
            selection->emitVisibleSheetRequested(region.firstSheet());
        }
        selection->initialize(region);
        return true;
    }

    // check whether a valid cell region was entered
    region = Region(text, selection->activeSheet()->map(), selection->activeSheet());
    if (region.isValid()) {
        // TODO Stefan: Merge the sheet change into Selection.
        if (region.firstSheet() != selection->activeSheet()) {
            selection->emitVisibleSheetRequested(region.firstSheet());
        }
        selection->initialize(region);
        return true;
    }

    // A name for an area entered?
    // FIXME Stefan: allow all characters
    bool validName = true;
    for (int i = 0; i < text.length(); ++i) {
        if (!text[i].isLetter()) {
            validName = false;
            break;
        }
    }
    if (validName) {
        NamedAreaCommand* command = new NamedAreaCommand();
        command->setSheet(selection->activeSheet());
        command->setAreaName(text);
        command->add(Region(selection->lastRange(), selection->activeSheet()));
        if (command->execute())
            return true;
        else
            delete command;
    }
    return false;
}


void LocationComboBox::keyPressEvent(QKeyEvent * _ev)
{
    if (!m_selection) return;

    Selection *const selection = m_selection;

    // Do not handle special keys and accelerators. This is
    // done by KComboBox.
    if (_ev->modifiers() & (Qt::AltModifier | Qt::ControlModifier)) {
        KComboBox::keyPressEvent(_ev);
        // Never allow that keys are passed on to the parent.
        _ev->accept(); // QKeyEvent

        return;
    }

    // Handle some special keys here. Eve
    switch (_ev->key()) {
    case Qt::Key_Return:
    case Qt::Key_Enter: {
        if (activateItem()) {
            selection->scrollToCursor();
            return;
        }
        _ev->accept(); // QKeyEvent
    }
    break;
    // Escape pressed, restore original value
    case Qt::Key_Escape:
        updateAddress();
        parentWidget()->setFocus();
        _ev->accept(); // QKeyEvent
        break;
    default:
        KComboBox::keyPressEvent(_ev);
        // Never allow that keys are passed on to the parent.
        _ev->accept(); // QKeyEvent
    }
}

void LocationComboBox::slotSelectionChanged()
{
    if (!m_selection->referenceSelectionMode()) {
        updateAddress();
    }
}

#include "LocationComboBox.moc"