File indexing completed on 2024-05-12 16:35:11
0001 /* This file is part of the KDE project 0002 Copyright 2007 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 #include "AutoFormatCommand.h" 0021 0022 #include "CellStorage.h" 0023 #include "Sheet.h" 0024 #include "Style.h" 0025 #include "Region.h" 0026 0027 #include <KLocalizedString> 0028 0029 #include <QPen> 0030 0031 using namespace Calligra::Sheets; 0032 0033 AutoFormatCommand::AutoFormatCommand() 0034 { 0035 setText(kundo2_i18n("Auto-Format")); 0036 } 0037 0038 AutoFormatCommand::~AutoFormatCommand() 0039 { 0040 } 0041 0042 void AutoFormatCommand::setStyles(const QList<Style>& styles) 0043 { 0044 m_styles = styles; 0045 } 0046 0047 bool AutoFormatCommand::preProcessing() 0048 { 0049 if (m_firstrun) 0050 m_sheet->cellStorage()->startUndoRecording(); 0051 // always reset the style of the processed region 0052 Style defaultStyle; 0053 defaultStyle.setDefault(); 0054 Region::ConstIterator end(constEnd()); 0055 for (Region::ConstIterator it = constBegin(); it != end; ++it) 0056 m_sheet->cellStorage()->setStyle(Region((*it)->rect()), defaultStyle); 0057 if (m_firstrun) 0058 m_sheet->cellStorage()->stopUndoRecording(this); 0059 return true; 0060 } 0061 0062 bool AutoFormatCommand::mainProcessing() 0063 { 0064 if (m_reverse) { 0065 KUndo2Command::undo(); // undo child commands 0066 return true; 0067 } 0068 return AbstractRegionCommand::mainProcessing(); 0069 } 0070 0071 bool AutoFormatCommand::process(Element* element) 0072 { 0073 const QRect rect = element->rect(); 0074 0075 // Top left corner 0076 if (!m_styles[0].isDefault()) 0077 m_sheet->cellStorage()->setStyle(Region(rect.topLeft()), m_styles[0]); 0078 // Top row 0079 for (int col = rect.left() + 1; col <= rect.right(); ++col) { 0080 int pos = 1 + ((col - rect.left() - 1) % 2); 0081 Cell cell(m_sheet, col, rect.top()); 0082 if (!cell.isPartOfMerged()) { 0083 Style style; 0084 if (!m_styles[pos].isDefault()) 0085 style = m_styles[pos]; 0086 0087 Style tmpStyle = (col == rect.left() + 1) ? m_styles[1] : m_styles[2]; 0088 if (!tmpStyle.isDefault()) 0089 style.setLeftBorderPen(tmpStyle.leftBorderPen()); 0090 0091 m_sheet->cellStorage()->setStyle(Region(col, rect.top()), style); 0092 } 0093 } 0094 0095 // Left column 0096 for (int row = rect.top() + 1; row <= rect.bottom(); ++row) { 0097 int pos = 4 + ((row - rect.top() - 1) % 2) * 4; 0098 Cell cell(m_sheet, rect.left(), row); 0099 if (!cell.isPartOfMerged()) { 0100 Style style; 0101 if (!m_styles[pos].isDefault()) 0102 style = m_styles[pos]; 0103 0104 Style tmpStyle = (row == rect.top() + 1) ? m_styles[4] : m_styles[8]; 0105 if (!tmpStyle.isDefault()) 0106 style.setTopBorderPen(tmpStyle.topBorderPen()); 0107 0108 m_sheet->cellStorage()->setStyle(Region(rect.left(), row), style); 0109 } 0110 } 0111 0112 // Body 0113 for (int col = rect.left() + 1; col <= rect.right(); ++col) { 0114 for (int row = rect.top() + 1; row <= rect.bottom(); ++row) { 0115 int pos = 5 + ((row - rect.top() - 1) % 2) * 4 + ((col - rect.left() - 1) % 2); 0116 Cell cell(m_sheet, col, row); 0117 if (!cell.isPartOfMerged()) { 0118 if (!m_styles[pos].isDefault()) 0119 m_sheet->cellStorage()->setStyle(Region(col, row), m_styles[pos]); 0120 0121 Style style; 0122 if (col == rect.left() + 1) 0123 style = m_styles[ 5 + ((row - rect.top() - 1) % 2) * 4 ]; 0124 else 0125 style = m_styles[ 6 + ((row - rect.top() - 1) % 2) * 4 ]; 0126 0127 if (!style.isDefault()) { 0128 Style tmpStyle; 0129 tmpStyle.setLeftBorderPen(style.leftBorderPen()); 0130 m_sheet->cellStorage()->setStyle(Region(col, row), tmpStyle); 0131 } 0132 0133 if (row == rect.top() + 1) 0134 style = m_styles[ 5 + ((col - rect.left() - 1) % 2)]; 0135 else 0136 style = m_styles[ 9 + ((col - rect.left() - 1) % 2)]; 0137 0138 if (!style.isDefault()) { 0139 Style tmpStyle; 0140 tmpStyle.setTopBorderPen(style.topBorderPen()); 0141 m_sheet->cellStorage()->setStyle(Region(col, row), tmpStyle); 0142 } 0143 } 0144 } 0145 } 0146 0147 // Outer right border 0148 for (int row = rect.top(); row <= rect.bottom(); ++row) { 0149 Cell cell(m_sheet, rect.right(), row); 0150 if (!cell.isPartOfMerged()) { 0151 if (row == rect.top()) { 0152 if (!m_styles[3].isDefault()) { 0153 Style tmpStyle; 0154 tmpStyle.setRightBorderPen(m_styles[3].leftBorderPen()); 0155 m_sheet->cellStorage()->setStyle(Region(rect.right(), row), tmpStyle); 0156 } 0157 } else if (row == rect.right()) { 0158 if (!m_styles[11].isDefault()) { 0159 Style tmpStyle; 0160 tmpStyle.setRightBorderPen(m_styles[11].leftBorderPen()); 0161 m_sheet->cellStorage()->setStyle(Region(rect.right(), row), tmpStyle); 0162 } 0163 } else { 0164 if (!m_styles[7].isDefault()) { 0165 Style tmpStyle; 0166 tmpStyle.setRightBorderPen(m_styles[7].leftBorderPen()); 0167 m_sheet->cellStorage()->setStyle(Region(rect.right(), row), tmpStyle); 0168 } 0169 } 0170 } 0171 } 0172 0173 // Outer bottom border 0174 for (int col = rect.left(); col <= rect.right(); ++col) { 0175 Cell cell(m_sheet, col, rect.bottom()); 0176 if (!cell.isPartOfMerged()) { 0177 if (col == rect.left()) { 0178 if (!m_styles[12].isDefault()) { 0179 Style tmpStyle; 0180 tmpStyle.setBottomBorderPen(m_styles[12].topBorderPen()); 0181 m_sheet->cellStorage()->setStyle(Region(col, rect.bottom()), tmpStyle); 0182 } 0183 } else if (col == rect.right()) { 0184 if (!m_styles[14].isDefault()) { 0185 Style tmpStyle; 0186 tmpStyle.setBottomBorderPen(m_styles[14].topBorderPen()); 0187 m_sheet->cellStorage()->setStyle(Region(col, rect.bottom()), tmpStyle); 0188 } 0189 } else { 0190 if (!m_styles[13].isDefault()) { 0191 Style tmpStyle; 0192 tmpStyle.setBottomBorderPen(m_styles[13].topBorderPen()); 0193 m_sheet->cellStorage()->setStyle(Region(col, rect.bottom()), tmpStyle); 0194 } 0195 } 0196 } 0197 } 0198 return true; 0199 }