1 /* This file is part of the KDE project
2    Copyright 2005-2006 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
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 // Local
21 #include "BorderColorCommand.h"
22 
23 #include <QPen>
24 
25 #include <KLocalizedString>
26 
27 #include "Cell.h"
28 #include "CellStorage.h"
29 #include "Sheet.h"
30 #include "Style.h"
31 #include "StyleStorage.h"
32 
33 using namespace Calligra::Sheets;
34 
BorderColorCommand()35 BorderColorCommand::BorderColorCommand()
36         : AbstractRegionCommand()
37 {
38     setText(kundo2_i18n("Change Border Color"));
39 }
40 
preProcessing()41 bool BorderColorCommand::preProcessing()
42 {
43     if (m_firstrun) {
44         QList< QPair<QRectF, SharedSubStyle> > undoData = m_sheet->styleStorage()->undoData(*this);
45         ConstIterator endOfList = constEnd();
46         for (ConstIterator it = constBegin(); it != endOfList; ++it) {
47             for (int i = 0; i < undoData.count(); ++i) {
48                 if (undoData[i].second->type() != Style::LeftPen &&
49                         undoData[i].second->type() != Style::RightPen &&
50                         undoData[i].second->type() != Style::TopPen &&
51                         undoData[i].second->type() != Style::BottomPen &&
52                         undoData[i].second->type() != Style::FallDiagonalPen &&
53                         undoData[i].second->type() != Style::GoUpDiagonalPen) {
54                     undoData.removeAt(i--);
55                 }
56             }
57             m_undoData += undoData;
58         }
59     }
60     return AbstractRegionCommand::preProcessing();
61 }
62 
mainProcessing()63 bool BorderColorCommand::mainProcessing()
64 {
65     if (!m_reverse) {
66         // change colors
67         Style style;
68         for (int i = 0; i < m_undoData.count(); ++i) {
69             style.clear();
70             style.insertSubStyle(m_undoData[i].second);
71             QPen pen;
72             if (m_undoData[i].second->type() == Style::LeftPen) {
73                 pen = style.leftBorderPen();
74                 pen.setColor(m_color);
75                 style.setLeftBorderPen(pen);
76             }
77             if (m_undoData[i].second->type() == Style::RightPen) {
78                 pen = style.rightBorderPen();
79                 pen.setColor(m_color);
80                 style.setRightBorderPen(pen);
81             }
82             if (m_undoData[i].second->type() == Style::TopPen) {
83                 pen = style.topBorderPen();
84                 pen.setColor(m_color);
85                 style.setTopBorderPen(pen);
86             }
87             if (m_undoData[i].second->type() == Style::BottomPen) {
88                 pen = style.bottomBorderPen();
89                 pen.setColor(m_color);
90                 style.setBottomBorderPen(pen);
91             }
92             if (m_undoData[i].second->type() == Style::FallDiagonalPen) {
93                 pen = style.fallDiagonalPen();
94                 pen.setColor(m_color);
95                 style.setFallDiagonalPen(pen);
96             }
97             if (m_undoData[i].second->type() == Style::GoUpDiagonalPen) {
98                 pen = style.goUpDiagonalPen();
99                 pen.setColor(m_color);
100                 style.setGoUpDiagonalPen(pen);
101             }
102             m_sheet->cellStorage()->setStyle(Region(m_undoData[i].first.toRect()), style);
103         }
104     } else { // m_reverse
105         for (int i = 0; i < m_undoData.count(); ++i) {
106             Style style;
107             style.insertSubStyle(m_undoData[i].second);
108             m_sheet->cellStorage()->setStyle(Region(m_undoData[i].first.toRect()), style);
109         }
110     }
111     return true;
112 }
113 
postProcessing()114 bool BorderColorCommand::postProcessing()
115 {
116     return true;
117 }
118