File indexing completed on 2024-05-12 16:36:10

0001 /* This file is part of the KDE project
0002    Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
0003    Copyright (C) 2005-2006 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
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 #ifndef CALLIGRA_SHEETS_SELECTION
0022 #define CALLIGRA_SHEETS_SELECTION
0023 
0024 #include <QColor>
0025 #include <QList>
0026 
0027 #include <KoToolSelection.h>
0028 
0029 #include "SheetsDebug.h"
0030 
0031 #include <sheets/Region.h>
0032 
0033 #include "sheets_common_export.h"
0034 
0035 class KoCanvasBase;
0036 
0037 namespace Calligra
0038 {
0039 namespace Sheets
0040 {
0041 
0042 /**
0043  * \class Selection
0044  * \brief Manages the selection of cells.
0045  * Represents cell selections for general operations and for cell references
0046  * used in formulaes.
0047  * \author Torben Weis <weis@kde.org>
0048  * \author Stefan Nikolaus <stefan.nikolaus@kdemail.net>
0049  */
0050 class CALLIGRA_SHEETS_COMMON_EXPORT Selection : public KoToolSelection, public Region
0051 {
0052     Q_OBJECT
0053 
0054 public:
0055     /**
0056      * The selection mode.
0057      */
0058     // TODO Stefan: merge with RegionSelector::SelectionMode
0059     enum Mode {
0060         SingleCell = 0,     ///< single cell selection mode
0061         MultipleCells = 1   ///< multiple cell selection mode
0062     };
0063 
0064     /**
0065      * Constructor.
0066      * Creates a new selection with (1,1) as initial location.
0067      * @param canvasBase the canvas interface
0068      */
0069     explicit Selection(KoCanvasBase* canvasBase);
0070 
0071     /**
0072      * Copy Constructor.
0073      * Creates a copy of @p selection
0074      * @param selection the Selection to copy
0075      */
0076     Selection(const Selection& selection);
0077 
0078     /**
0079      * Destructor.
0080      */
0081     ~Selection() override;
0082 
0083     /**
0084      * \return the canvas this selection works for.
0085      */
0086     KoCanvasBase* canvas() const;
0087 
0088     /**
0089      * Sets the selection to @p point
0090      * @param point the point's location
0091      * @param sheet the sheet the point belongs to
0092      */
0093     void initialize(const QPoint& point, Sheet* sheet = 0);
0094 
0095     /**
0096      * Sets the selection to @p range
0097      * @param range the range's location
0098      * @param sheet the sheet the range belongs to
0099      */
0100     void initialize(const QRect& range, Sheet* sheet = 0);
0101 
0102     /**
0103      * Sets the selection to @p region
0104      * @param region the region's locations
0105      * @param sheet the sheet the region belongs to
0106      */
0107     void initialize(const Region& region, Sheet* sheet = 0);
0108 
0109     /**
0110      * Emits signal changed(const Region&)
0111      */
0112     void update();
0113 
0114     /**
0115      * Update the marker of the selection to @p point .
0116      * Uses the anchor as starting point
0117      * @p point the new marker location
0118      */
0119     void update(const QPoint& point);
0120 
0121     /**
0122      * Extends the current selection with the Point @p point
0123      * @param point the point's location
0124      * @param sheet the sheet the point belongs to
0125      */
0126     void extend(const QPoint& point, Sheet* sheet = 0);
0127 
0128     /**
0129      * Extends the current selection with the Range @p range
0130      * @param range the range's location
0131      * @param sheet the sheet the range belongs to
0132      */
0133     void extend(const QRect& range, Sheet* sheet = 0);
0134 
0135     /**
0136      * Extends the current selection with the Region @p region
0137      * @param region the region's locations
0138      */
0139     void extend(const Region& region);
0140 
0141     /**
0142      * @param point the point's location
0143      * @param sheet the sheet the point belongs to
0144      */
0145     Element* eor(const QPoint& point, Sheet* sheet = 0) override;
0146 
0147     /**
0148      * The anchor is the starting point of a range. For points marker and anchor are the same
0149      */
0150     const QPoint& anchor() const;
0151 
0152     /**
0153      * The cursor represents the cursor position. This is needed for merged cells
0154      */
0155     const QPoint& cursor() const;
0156 
0157     /**
0158      * The marker is the end point of a range. For points marker and anchor are the same
0159      */
0160     const QPoint& marker() const;
0161 
0162     /**
0163      * Checks whether the region consists only of one point
0164      */
0165     bool isSingular() const;
0166 
0167     /**
0168      * @return the name of the region (e.g. "A1:A2")
0169      */
0170     QString name(Sheet* originSheet = 0) const;
0171 
0172     /**
0173      * Sets the selection's active sheet.
0174      * For usual selections this is always the origin sheet,
0175      * but for cell choices used for formulaes it may differ.
0176      * @param sheet the sheet which is currently active
0177      */
0178     void setActiveSheet(Sheet* sheet);
0179 
0180     /**
0181      * @return the selection's active sheet
0182      */
0183     Sheet* activeSheet() const;
0184 
0185     /**
0186      * Sets the selection's origin sheet.
0187      * @param sheet the sheet from which the selection starts
0188      */
0189     void setOriginSheet(Sheet* sheet);
0190 
0191     /**
0192      * @return the selection's origin sheet
0193      */
0194     Sheet* originSheet() const;
0195 
0196     /**
0197      * Activates the cell location/range, that has \p cell as bottom left or
0198      * right cell. If more than one occurrence would fit, the first one is taken.
0199      * \return the index of the activated range or \c -1, if nothing fits
0200      */
0201     int setActiveElement(const Cell &cell);
0202 
0203     /**
0204      * @return the active element
0205      */
0206     Element* activeElement() const;
0207 
0208     /**
0209      * Sets the starting position and the length of a sub-region.
0210      * On inserting/updating the selection the sub-region gets replaced
0211      * by the new cell location/range.
0212      * A \p length of \c 0 results in no replacement, but just in inserting the
0213      * new cell location/range before the range index \p start.
0214      * \param start The index of a range in this selection. It has to be a valid
0215      * index; otherwise the sub-region will be set to the whole region.
0216      * \param length The amount of ranges in the sub-region. If it exceeds the
0217      * amount of ranges, beginning from \p start to the end of range list, it
0218      * will be adjusted.
0219      * \param active The active element within the sub-region.
0220      * \verbatim start <= active <= start + length \endverbatim
0221      */
0222     void setActiveSubRegion(int start, int length, int active = -1);
0223 
0224     /**
0225      *
0226      */
0227     QString activeSubRegionName() const;
0228 
0229     /**
0230      * Clears the elements of the subregion
0231      */
0232     void clearSubRegion();
0233 
0234     /**
0235      * fix subregion dimensions
0236      */
0237     void fixSubRegionDimension();
0238 
0239     /**
0240      * Deletes all elements of the region. The result is an empty region.
0241      */
0242     void clear() override;
0243 
0244 
0245     /**
0246      * \param mode single cell or multiple cell selection
0247      */
0248     void setSelectionMode(Mode mode);
0249 
0250     /**
0251      * Extends \p area to include the merged cells, that are not fully covered,
0252      * completely.
0253      * \return the extended area
0254      */
0255     QRect extendToMergedAreas(const QRect& area) const;
0256 
0257     const QList<QColor>& colors() const;
0258 
0259     void selectAll();
0260 
0261     /** Start using a reference selection instead of normal one. */
0262     void startReferenceSelection();
0263     /** End using reference selection. */
0264     void endReferenceSelection(bool saveChanges = true);
0265     /** Enable/disable reference choosing mode. */
0266     void setReferenceSelectionMode(bool enable);
0267     /** Are we in reference choosing mode ? */
0268     bool referenceSelectionMode() const;
0269     /** Are we currently using a reference selection ? */
0270     bool referenceSelection() const;
0271 
0272     void emitAboutToModify();
0273     void emitModified();
0274     void emitRefreshSheetViews();
0275     void emitVisibleSheetRequested(Sheet* sheet);
0276     void emitCloseEditor(bool saveChanges, bool expandMatrix = false);
0277     void emitRequestFocusEditor();
0278 
0279     void scrollToCursor();
0280 Q_SIGNALS:
0281     /**
0282      * Emitted when the Selection was changed.
0283      * @param region the changed part of the Selection
0284      */
0285     void changed(const Region& region);
0286 
0287     /**
0288      * An operation on the selection is about to happen.
0289      */
0290     void aboutToModify(const Region& region);
0291 
0292     /**
0293      * Emitted when the content was modified.
0294      */
0295     void modified(const Region& region);
0296 
0297     void refreshSheetViews();
0298     void visibleSheetRequested(Sheet* sheet);
0299     void closeEditor(bool saveChanges, bool expandMatrix);
0300     void activeSheetChanged(Sheet* sheet);
0301     void requestFocusEditor();
0302 
0303     void documentReadWriteToggled(bool readWrite);
0304     void sheetProtectionToggled(bool protect);
0305 
0306     void updateAccessedCellRange(Sheet* sheet, const QPoint& location);
0307 
0308 protected:
0309     class Point;
0310     class Range;
0311 
0312     /**
0313      * @internal used to create derived Points
0314      */
0315     Region::Point* createPoint(const QPoint&) const override;
0316 
0317     /**
0318      * @internal used to create derived Points
0319      */
0320     Region::Point* createPoint(const QString&) const override;
0321 
0322     /**
0323      * @internal used to create derived Points
0324      */
0325     Region::Point* createPoint(const Region::Point&) const override;
0326 
0327     /**
0328      * @internal used to create derived Ranges
0329      */
0330     Region::Range* createRange(const QRect&) const override;
0331 
0332     /**
0333      * @internal used to create derived Ranges
0334      */
0335     Region::Range* createRange(const Region::Point&, const Region::Point&) const override;
0336 
0337     /**
0338      * @internal used to create derived Ranges
0339      */
0340     Region::Range* createRange(const QString&) const override;
0341 
0342     /**
0343      * @internal used to create derived Ranges
0344      */
0345     Region::Range* createRange(const Region::Range&) const override;
0346 
0347     /**
0348      * Dilates the region and emits the changed() signal.
0349      * \internal
0350      */
0351     void emitChanged(const Region& changedRegion);
0352 
0353     /**
0354      * @internal
0355      */
0356     void dump() const;
0357 
0358 private:
0359     // do not allow assignment
0360     Selection& operator=(const Selection&);
0361 
0362     class Private;
0363     Private * const d;
0364 };
0365 
0366 /***************************************************************************
0367   class Selection::Point
0368 ****************************************************************************/
0369 
0370 /**
0371  * This Point is extended by an color attribute.
0372  */
0373 class Selection::Point : public Region::Point
0374 {
0375 public:
0376     Point(const QPoint& point);
0377     Point(const QString& string);
0378     Point(const Region::Point& point);
0379 
0380     void setColor(const QColor& color) {
0381         m_color = color;
0382     }
0383     virtual const QColor& color() const {
0384         return m_color;
0385     }
0386 
0387 private:
0388     QColor m_color;
0389 };
0390 
0391 /***************************************************************************
0392   class Selection::Range
0393 ****************************************************************************/
0394 
0395 /**
0396  * This Range is extended by an color attribute.
0397  */
0398 class Selection::Range : public Region::Range
0399 {
0400 public:
0401     Range(const QRect& rect);
0402     Range(const Calligra::Sheets::Region::Point& tl, const Calligra::Sheets::Region::Point& br);
0403     Range(const QString& string);
0404     Range(const Region::Range& range);
0405 
0406     void setColor(const QColor& color) {
0407         m_color = color;
0408     }
0409     const QColor& color() const {
0410         return m_color;
0411     }
0412 
0413 private:
0414     QColor m_color;
0415 };
0416 
0417 } // namespace Sheets
0418 } // namespace Calligra
0419 
0420 #endif // CALLIGRA_SHEETS_SELECTION