1 /* This file is part of the KDE project
2    Copyright (C) 2003 Norbert Andres <nandres@web.de>
3              (C) 2000-2002 Laurent Montel <montel@kde.org>
4              (C) 2001-2002 Philipp Mueller <philipp.mueller@gmx.de>
5              (C) 2002 John Dailey <dailey@vt.edu>
6 
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Library General Public
9    License as published by the Free Software Foundation; either
10    version 2 of the License, or (at your option) any later version.
11 
12    This library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Library General Public License for more details.
16 
17    You should have received a copy of the GNU Library General Public License
18    along with this library; see the file COPYING.LIB.  If not, write to
19    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20    Boston, MA 02110-1301, USA.
21 */
22 
23 // Local
24 #include "ShowColRowDialog.h"
25 
26 // Qt
27 #include <QLabel>
28 #include <QVBoxLayout>
29 #include <QListWidget>
30 
31 // KF5
32 #include <KLocalizedString>
33 
34 // Sheets
35 #include "calligra_sheets_limits.h"
36 #include "Region.h"
37 #include "RowColumnFormat.h"
38 #include "RowFormatStorage.h"
39 #include "ui/Selection.h"
40 #include "Sheet.h"
41 
42 // commands
43 #include "commands/RowColumnManipulators.h"
44 
45 // Other
46 #include <algorithm>
47 
48 using namespace Calligra::Sheets;
49 
ShowColRow(QWidget * parent,Selection * selection,Type _type)50 ShowColRow::ShowColRow(QWidget* parent, Selection* selection, Type _type)
51         : KoDialog(parent)
52 {
53     setModal(true);
54     setButtons(Ok | Cancel);
55     m_selection = selection;
56     typeShow = _type;
57 
58     QWidget *page = new QWidget();
59     setMainWidget(page);
60     QVBoxLayout *lay1 = new QVBoxLayout(page);
61 
62     QLabel *label = new QLabel(page);
63 
64     if (_type == Column) {
65         setWindowTitle(i18n("Show Columns"));
66         label->setText(i18n("Select hidden columns to show:"));
67     } else if (_type == Row) {
68         setWindowTitle(i18n("Show Rows"));
69         label->setText(i18n("Select hidden rows to show:"));
70     }
71 
72     list = new QListWidget(page);
73 
74     lay1->addWidget(label);
75     lay1->addWidget(list);
76 
77     bool showColNumber = m_selection->activeSheet()->getShowColumnNumber();
78     if (_type == Column) {
79         ColumnFormat *col = m_selection->activeSheet()->firstCol();
80 
81         QString text;
82         QStringList listCol;
83         for (; col; col = col->next()) {
84             if (col->isHidden())
85                 listInt.append(col->column());
86         }
87         std::sort(listInt.begin(), listInt.end());
88         for (QList<int>::ConstIterator it = listInt.constBegin(); it != listInt.constEnd(); ++it) {
89             if (!showColNumber)
90                 listCol += i18n("Column: %1", Cell::columnName(*it));
91             else
92                 listCol += i18n("Column: %1", text.setNum(*it));
93         }
94         list->addItems(listCol);
95     } else if (_type == Row) {
96         QString text;
97         QStringList listRow;
98         int lastRow, row = 1;
99         while (row <= KS_rowMax) {
100             if (m_selection->activeSheet()->rowFormats()->isHidden(row, &lastRow)) {
101                 for (int i = row; i <= lastRow; ++i)
102                     listInt.append(i);
103             }
104             row = lastRow+1;
105         }
106         for (QList<int>::ConstIterator it = listInt.constBegin(); it != listInt.constEnd(); ++it)
107             listRow += i18n("Row: %1", text.setNum(*it));
108 
109         list->addItems(listRow);
110     }
111 
112     if (!list->count())
113         enableButtonOk(false);
114 
115     //selection multiple
116     list->setSelectionMode(QAbstractItemView::MultiSelection);
117     connect(this, SIGNAL(okClicked()), this, SLOT(slotOk()));
118     connect(list, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(slotDoubleClicked(QListWidgetItem*)));
119     resize(200, 150);
120     setFocus();
121 }
122 
slotDoubleClicked(QListWidgetItem *)123 void ShowColRow::slotDoubleClicked(QListWidgetItem *)
124 {
125     slotOk();
126 }
127 
slotOk()128 void ShowColRow::slotOk()
129 {
130     Region region;
131     for (unsigned int i = 0; i < (unsigned int)list->count(); i++) {
132         if (list->item(i)->isSelected()) {
133             if (typeShow == Column) {
134                 region.add(QRect(listInt.at(i), 1, 1, KS_rowMax));
135             }
136             if (typeShow == Row) {
137                 region.add(QRect(1, listInt.at(i), KS_colMax, 1));
138             }
139         }
140     }
141 
142     HideShowManipulator* manipulator = new HideShowManipulator();
143     manipulator->setSheet(m_selection->activeSheet());
144     if (typeShow == Column) {
145         manipulator->setManipulateColumns(true);
146     }
147     if (typeShow == Row) {
148         manipulator->setManipulateRows(true);
149     }
150     manipulator->setReverse(true);
151     manipulator->add(region);
152     manipulator->execute(m_selection->canvas());
153 
154     accept();
155 }
156