kspread

AutoFormatCommand.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "AutoFormatCommand.h"
00021 
00022 #include "CellStorage.h"
00023 #include "Sheet.h"
00024 #include "Style.h"
00025 #include "Region.h"
00026 
00027 #include <klocale.h>
00028 
00029 #include <QPen>
00030 
00031 #include "CellStorage_p.h"
00032 
00033 using namespace KSpread;
00034 
00035 AutoFormatCommand::AutoFormatCommand()
00036 {
00037     setText(i18n("Auto-Format"));
00038 }
00039 
00040 AutoFormatCommand::~AutoFormatCommand()
00041 {
00042     delete m_undoData;
00043 }
00044 
00045 void AutoFormatCommand::setStyles(const QList<Style>& styles)
00046 {
00047     m_styles = styles;
00048 }
00049 
00050 bool AutoFormatCommand::preProcessing()
00051 {
00052     if (m_firstrun)
00053         m_sheet->cellStorage()->startUndoRecording();
00054     // always reset the style of the processed region
00055     Style defaultStyle;
00056     defaultStyle.setDefault();
00057     Region::ConstIterator end(constEnd());
00058     for (Region::ConstIterator it = constBegin(); it != end; ++it)
00059         m_sheet->cellStorage()->setStyle(Region((*it)->rect()), defaultStyle);
00060     if (m_firstrun)
00061         m_undoData = m_sheet->cellStorage()->stopUndoRecording();
00062     return true;
00063 }
00064 
00065 bool AutoFormatCommand::mainProcessing()
00066 {
00067     if (m_reverse)
00068     {
00069         m_sheet->cellStorage()->undo(m_undoData);
00070         return true;
00071     }
00072     return AbstractRegionCommand::mainProcessing();
00073 }
00074 
00075 bool AutoFormatCommand::process(Element* element)
00076 {
00077     const QRect rect = element->rect();
00078 
00079     // Top left corner
00080     if (!m_styles[0].isDefault())
00081         m_sheet->cellStorage()->setStyle(Region(rect.topLeft()), m_styles[0]);
00082     // Top row
00083     for (int col = rect.left() + 1; col <= rect.right(); ++col)
00084     {
00085         int pos = 1 + ((col - rect.left() - 1) % 2);
00086         Cell cell(m_sheet, col, rect.top());
00087         if (!cell.isPartOfMerged())
00088         {
00089             Style style;
00090             if (!m_styles[pos].isDefault())
00091                 style = m_styles[pos];
00092 
00093             Style tmpStyle = (col == rect.left() + 1) ? m_styles[1] : m_styles[2];
00094             if (!tmpStyle.isDefault())
00095                 style.setLeftBorderPen(tmpStyle.leftBorderPen());
00096 
00097             m_sheet->cellStorage()->setStyle(Region(col, rect.top()), style);
00098         }
00099     }
00100 
00101     // Left column
00102     for (int row = rect.top() + 1; row <= rect.bottom(); ++row)
00103     {
00104         int pos = 4 + ((row - rect.top() - 1) % 2) * 4;
00105         Cell cell(m_sheet, rect.left(), row);
00106         if (!cell.isPartOfMerged())
00107         {
00108             Style style;
00109             if (!m_styles[pos].isDefault())
00110                 style = m_styles[pos];
00111 
00112             Style tmpStyle = (row == rect.top() + 1) ? m_styles[4] : m_styles[8];
00113             if (!tmpStyle.isDefault())
00114                 style.setTopBorderPen(tmpStyle.topBorderPen());
00115 
00116             m_sheet->cellStorage()->setStyle(Region(rect.left(), row), style);
00117         }
00118     }
00119 
00120     // Body
00121     for (int col = rect.left() + 1; col <= rect.right(); ++col)
00122     {
00123         for (int row = rect.top() + 1; row <= rect.bottom(); ++row)
00124         {
00125             int pos = 5 + ((row - rect.top() - 1) % 2) * 4 + ((col - rect.left() - 1) % 2);
00126             Cell cell(m_sheet, col, row);
00127             if (!cell.isPartOfMerged())
00128             {
00129                 if (!m_styles[pos].isDefault())
00130                     m_sheet->cellStorage()->setStyle(Region(col, row), m_styles[pos]);
00131 
00132                 Style style;
00133                 if (col == rect.left() + 1)
00134                     style = m_styles[ 5 + ((row - rect.top() - 1) % 2) * 4 ];
00135                 else
00136                     style = m_styles[ 6 + ((row - rect.top() - 1) % 2) * 4 ];
00137 
00138                 if (!style.isDefault())
00139                 {
00140                     Style tmpStyle;
00141                     tmpStyle.setLeftBorderPen(style.leftBorderPen());
00142                     m_sheet->cellStorage()->setStyle(Region(col, row), tmpStyle);
00143                 }
00144 
00145                 if (row == rect.top() + 1)
00146                     style = m_styles[ 5 + ((col - rect.left() - 1) % 2) ];
00147                 else
00148                     style = m_styles[ 9 + ((col - rect.left() - 1) % 2) ];
00149 
00150                 if (!style.isDefault())
00151                 {
00152                     Style tmpStyle;
00153                     tmpStyle.setTopBorderPen(style.topBorderPen());
00154                     m_sheet->cellStorage()->setStyle(Region(col, row), tmpStyle);
00155                 }
00156             }
00157         }
00158     }
00159 
00160     // Outer right border
00161     for (int row = rect.top(); row <= rect.bottom(); ++row)
00162     {
00163         Cell cell(m_sheet, rect.right(), row);
00164         if (!cell.isPartOfMerged())
00165         {
00166             if (row == rect.top())
00167             {
00168                 if (!m_styles[3].isDefault())
00169                 {
00170                     Style tmpStyle;
00171                     tmpStyle.setRightBorderPen(m_styles[3].leftBorderPen());
00172                     m_sheet->cellStorage()->setStyle(Region(rect.right(), row), tmpStyle);
00173                 }
00174             }
00175             else if (row == rect.right())
00176             {
00177                 if (!m_styles[11].isDefault())
00178                 {
00179                     Style tmpStyle;
00180                     tmpStyle.setRightBorderPen(m_styles[11].leftBorderPen());
00181                     m_sheet->cellStorage()->setStyle(Region(rect.right(), row), tmpStyle);
00182                 }
00183             }
00184             else
00185             {
00186                 if (!m_styles[7].isDefault())
00187                 {
00188                     Style tmpStyle;
00189                     tmpStyle.setRightBorderPen(m_styles[7].leftBorderPen());
00190                     m_sheet->cellStorage()->setStyle(Region(rect.right(), row), tmpStyle);
00191                 }
00192             }
00193         }
00194     }
00195 
00196     // Outer bottom border
00197     for (int col = rect.left(); col <= rect.right(); ++col)
00198     {
00199         Cell cell(m_sheet, col, rect.bottom());
00200         if(!cell.isPartOfMerged())
00201         {
00202             if (col == rect.left())
00203             {
00204                 if (!m_styles[12].isDefault())
00205                 {
00206                     Style tmpStyle;
00207                     tmpStyle.setBottomBorderPen(m_styles[12].topBorderPen());
00208                     m_sheet->cellStorage()->setStyle(Region(col, rect.bottom()), tmpStyle);
00209                 }
00210             }
00211             else if (col == rect.right())
00212             {
00213                 if (!m_styles[14].isDefault())
00214                 {
00215                     Style tmpStyle;
00216                     tmpStyle.setBottomBorderPen(m_styles[14].topBorderPen());
00217                     m_sheet->cellStorage()->setStyle(Region(col, rect.bottom()), tmpStyle);
00218                 }
00219             }
00220             else
00221             {
00222                 if (!m_styles[13].isDefault())
00223                 {
00224                     Style tmpStyle;
00225                     tmpStyle.setBottomBorderPen(m_styles[13].topBorderPen());
00226                     m_sheet->cellStorage()->setStyle(Region(col, rect.bottom()), tmpStyle);
00227                 }
00228             }
00229         }
00230     }
00231     return true;
00232 }