File indexing completed on 2024-05-12 16:35:11

0001 /* This file is part of the KDE project
0002    Copyright 2005-2006 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 // Local
0021 #include "BorderColorCommand.h"
0022 
0023 #include <QPen>
0024 
0025 #include <KLocalizedString>
0026 
0027 #include "Cell.h"
0028 #include "CellStorage.h"
0029 #include "Sheet.h"
0030 #include "Style.h"
0031 #include "StyleStorage.h"
0032 
0033 using namespace Calligra::Sheets;
0034 
0035 BorderColorCommand::BorderColorCommand()
0036         : AbstractRegionCommand()
0037 {
0038     setText(kundo2_i18n("Change Border Color"));
0039 }
0040 
0041 bool BorderColorCommand::preProcessing()
0042 {
0043     if (m_firstrun) {
0044         QList< QPair<QRectF, SharedSubStyle> > undoData = m_sheet->styleStorage()->undoData(*this);
0045         ConstIterator endOfList = constEnd();
0046         for (ConstIterator it = constBegin(); it != endOfList; ++it) {
0047             for (int i = 0; i < undoData.count(); ++i) {
0048                 if (undoData[i].second->type() != Style::LeftPen &&
0049                         undoData[i].second->type() != Style::RightPen &&
0050                         undoData[i].second->type() != Style::TopPen &&
0051                         undoData[i].second->type() != Style::BottomPen &&
0052                         undoData[i].second->type() != Style::FallDiagonalPen &&
0053                         undoData[i].second->type() != Style::GoUpDiagonalPen) {
0054                     undoData.removeAt(i--);
0055                 }
0056             }
0057             m_undoData += undoData;
0058         }
0059     }
0060     return AbstractRegionCommand::preProcessing();
0061 }
0062 
0063 bool BorderColorCommand::mainProcessing()
0064 {
0065     if (!m_reverse) {
0066         // change colors
0067         Style style;
0068         for (int i = 0; i < m_undoData.count(); ++i) {
0069             style.clear();
0070             style.insertSubStyle(m_undoData[i].second);
0071             QPen pen;
0072             if (m_undoData[i].second->type() == Style::LeftPen) {
0073                 pen = style.leftBorderPen();
0074                 pen.setColor(m_color);
0075                 style.setLeftBorderPen(pen);
0076             }
0077             if (m_undoData[i].second->type() == Style::RightPen) {
0078                 pen = style.rightBorderPen();
0079                 pen.setColor(m_color);
0080                 style.setRightBorderPen(pen);
0081             }
0082             if (m_undoData[i].second->type() == Style::TopPen) {
0083                 pen = style.topBorderPen();
0084                 pen.setColor(m_color);
0085                 style.setTopBorderPen(pen);
0086             }
0087             if (m_undoData[i].second->type() == Style::BottomPen) {
0088                 pen = style.bottomBorderPen();
0089                 pen.setColor(m_color);
0090                 style.setBottomBorderPen(pen);
0091             }
0092             if (m_undoData[i].second->type() == Style::FallDiagonalPen) {
0093                 pen = style.fallDiagonalPen();
0094                 pen.setColor(m_color);
0095                 style.setFallDiagonalPen(pen);
0096             }
0097             if (m_undoData[i].second->type() == Style::GoUpDiagonalPen) {
0098                 pen = style.goUpDiagonalPen();
0099                 pen.setColor(m_color);
0100                 style.setGoUpDiagonalPen(pen);
0101             }
0102             m_sheet->cellStorage()->setStyle(Region(m_undoData[i].first.toRect()), style);
0103         }
0104     } else { // m_reverse
0105         for (int i = 0; i < m_undoData.count(); ++i) {
0106             Style style;
0107             style.insertSubStyle(m_undoData[i].second);
0108             m_sheet->cellStorage()->setStyle(Region(m_undoData[i].first.toRect()), style);
0109         }
0110     }
0111     return true;
0112 }
0113 
0114 bool BorderColorCommand::postProcessing()
0115 {
0116     return true;
0117 }