1 /* This file is part of the KDE project
2  * Copyright (C) 2010 C. Boemann <cbo@boemann.dk>
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 #include "SimpleTableWidget.h"
20 #include "TextTool.h"
21 
22 #include <KoTableCellStyle.h>
23 #include <KoTextLayoutCellHelper.h>
24 #include <KoZoomHandler.h>
25 #include <KoColorPopupAction.h>
26 #include <KoColor.h>
27 #include <KoIcon.h>
28 
29 #include <QAction>
30 #include <QDebug>
31 
32 #include <QWidget>
33 #include <QPixmap>
34 
SimpleTableWidget(TextTool * tool,QWidget * parent)35 SimpleTableWidget::SimpleTableWidget(TextTool *tool, QWidget *parent)
36     : QWidget(parent)
37     , m_blockSignals(false)
38     , m_tool(tool)
39     , m_lastStyleEmitted(2)
40 {
41     widget.setupUi(this);
42     widget.addRowAbove->setDefaultAction(tool->action("insert_tablerow_above"));
43     widget.addRowBelow->setDefaultAction(tool->action("insert_tablerow_below"));
44     widget.addColumnLeft->setDefaultAction(tool->action("insert_tablecolumn_left"));
45     widget.addColumnRight->setDefaultAction(tool->action("insert_tablecolumn_right"));
46     widget.deleteRow->setDefaultAction(tool->action("delete_tablerow"));
47     widget.deleteColumn->setDefaultAction(tool->action("delete_tablecolumn"));
48     widget.mergeCells->setDefaultAction(tool->action("merge_tablecells"));
49     widget.splitCells->setDefaultAction(tool->action("split_tablecells"));
50 
51     connect(tool->action("activate_borderpainter"), SIGNAL(triggered(bool)), this, SLOT(restartPainting()));
52     widget.border->setDefaultAction(tool->action("activate_borderpainter"));
53 
54     widget.border->setNumColumns(9);
55 
56     fillBorderButton(QColor(0, 0, 0));
57 
58     KoColorPopupAction *actionBorderColor = new KoColorPopupAction(this);
59     actionBorderColor->setIcon(koIcon("format-fill-color"));
60     actionBorderColor->setText(i18n("Set Border Color..."));
61     widget.border->addAction(actionBorderColor);
62     connect(actionBorderColor, SIGNAL(colorChanged(KoColor)), this, SLOT(setBorderColor(KoColor)));
63 
64     connect(widget.addRowAbove, SIGNAL(clicked(bool)), this, SIGNAL(doneWithFocus()));
65     connect(widget.addRowBelow, SIGNAL(clicked(bool)), this, SIGNAL(doneWithFocus()));
66     connect(widget.addColumnLeft, SIGNAL(clicked(bool)), this, SIGNAL(doneWithFocus()));
67     connect(widget.addColumnRight, SIGNAL(clicked(bool)), this, SIGNAL(doneWithFocus()));
68     connect(widget.deleteRow, SIGNAL(clicked(bool)), this, SIGNAL(doneWithFocus()));
69     connect(widget.deleteColumn, SIGNAL(clicked(bool)), this, SIGNAL(doneWithFocus()));
70     connect(widget.mergeCells, SIGNAL(clicked(bool)), this, SIGNAL(doneWithFocus()));
71     connect(widget.splitCells, SIGNAL(clicked(bool)), this, SIGNAL(doneWithFocus()));
72     connect(widget.border, SIGNAL(itemTriggered(int)), this, SLOT(emitTableBorderDataUpdated(int)));
73     connect(widget.border, SIGNAL(clicked(bool)), this, SIGNAL(doneWithFocus()));
74     connect(widget.border, SIGNAL(doneWithFocus()), this, SIGNAL(doneWithFocus()));
75 }
76 
restartPainting()77 void SimpleTableWidget::restartPainting()
78 {
79     emitTableBorderDataUpdated(m_lastStyleEmitted);
80 }
81 
emitTableBorderDataUpdated(int i)82 void SimpleTableWidget::emitTableBorderDataUpdated(int i)
83 {
84     m_lastStyleEmitted = i;
85     emit tableBorderDataUpdated(m_cellStyles[i - 1]->getEdge(KoBorder::TopBorder));
86 }
87 
setStyleManager(KoStyleManager * sm)88 void SimpleTableWidget::setStyleManager(KoStyleManager *sm)
89 {
90     m_styleManager = sm;
91 }
92 
setBorderColor(const KoColor & koColor)93 void SimpleTableWidget::setBorderColor(const KoColor &koColor)
94 {
95     fillBorderButton(koColor.toQColor());
96 }
97 
fillBorderButton(const QColor & color)98 void SimpleTableWidget::fillBorderButton(const QColor &color)
99 {
100     qDeleteAll(m_cellStyles);
101     m_cellStyles.clear();
102 
103     qreal thickness[9] = {0.25, 0.5, 0.75, 1.0, 1.5, 2.25, 3.0, 4.5, 6.0};
104 
105     KoTableCellStyle cellStyle;
106     qDeleteAll(m_cellStyles);
107     m_cellStyles.append(KoTableCellStyle().clone());
108     for (int i = 8; i < 9; i++) {
109         cellStyle.setEdge(KoBorder::TopBorder, KoBorder::BorderDoubleWave, thickness[i], color);
110         m_cellStyles.append(cellStyle.clone());
111     }
112     for (int i = 6; i < 8; i++) {
113         cellStyle.setEdge(KoBorder::TopBorder, KoBorder::BorderWave, thickness[i], color);
114         m_cellStyles.append(cellStyle.clone());
115     }
116     for (int i = 4; i < 9; i++) {
117         cellStyle.setEdge(KoBorder::TopBorder, KoBorder::BorderDouble, thickness[i] * 1.5, color);
118         cellStyle.setEdgeDoubleBorderValues(KoBorder::TopBorder, thickness[i], thickness[i] / 4);
119         m_cellStyles.append(cellStyle.clone());
120     }
121     for (int i = 6; i < 7; i++) {
122         cellStyle.setEdge(KoBorder::TopBorder, KoBorder::BorderSlash, thickness[i], color);
123         m_cellStyles.append(cellStyle.clone());
124     }
125     m_cellStyles.append(0);
126     for (int i = 0; i < 7; i++) {
127         cellStyle.setEdge(KoBorder::TopBorder, KoBorder::BorderDouble, thickness[i] * 3, color);
128         m_cellStyles.append(cellStyle.clone());
129     }
130     for (int i = 0; i < 9; i++) {
131         cellStyle.setEdge(KoBorder::TopBorder, KoBorder::BorderDouble, thickness[i] * 2, color);
132         cellStyle.setEdgeDoubleBorderValues(KoBorder::TopBorder, thickness[i] / 2, thickness[i] / 2);
133         m_cellStyles.append(cellStyle.clone());
134     }
135     for (int i = 0; i < 9; i++) {
136         cellStyle.setEdge(KoBorder::TopBorder, KoBorder::BorderSolid, thickness[i], color);
137         m_cellStyles.append(cellStyle.clone());
138     }
139     for (int i = 0; i < 9; i++) {
140         cellStyle.setEdge(KoBorder::TopBorder, KoBorder::BorderDotted, thickness[i], color);
141         m_cellStyles.append(cellStyle.clone());
142     }
143     for (int i = 0; i < 9; i++) {
144         cellStyle.setEdge(KoBorder::TopBorder, KoBorder::BorderDashed, thickness[i], color);
145         m_cellStyles.append(cellStyle.clone());
146     }
147     for (int i = 0; i < 9; i++) {
148         cellStyle.setEdge(KoBorder::TopBorder, KoBorder::BorderDashedLong, thickness[i], color);
149         m_cellStyles.append(cellStyle.clone());
150     }
151     for (int i = 0; i < 9; i++) {
152         cellStyle.setEdge(KoBorder::TopBorder, KoBorder::BorderDashDot, thickness[i], color);
153         m_cellStyles.append(cellStyle.clone());
154     }
155     for (int i = 0; i < 9; i++) {
156         cellStyle.setEdge(KoBorder::TopBorder, KoBorder::BorderDashDotDot, thickness[i], color);
157         m_cellStyles.append(cellStyle.clone());
158     }
159 
160     int i = 1;
161     KoZoomHandler zoomHandler;
162     Q_FOREACH (KoTableCellStyle *style, m_cellStyles) {
163         if (style == 0) {
164             widget.border->addBlanks(1);
165             i++;
166             continue;
167         }
168         QPixmap pm(48, 16);
169 
170         pm.fill(Qt::transparent);
171         QPainter p(&pm);
172         p.setRenderHint(QPainter::Antialiasing);
173 
174         if (style->hasBorders()) {
175             p.scale(zoomHandler.zoomedResolutionX(), zoomHandler.zoomedResolutionY());
176             KoTextLayoutCellHelper cellStyleHelper(*style);
177             qreal width = style->topBorderWidth();
178             cellStyleHelper.drawTopHorizontalBorder(p, 0, 8 / zoomHandler.zoomedResolutionY() - width / 2, pm.width() / zoomHandler.zoomedResolutionX(), 0);
179 
180             widget.border->addItem(pm, i, KoUnit().toUserStringValue(style->topBorderWidth()) + "pt");
181         } else {
182             p.drawText(0, 0, 48, 16, Qt::AlignCenter, i18nc("No border - has to fit in 48pixels", "None"));
183             widget.border->addItem(pm, i, i18n("No Border"));
184         }
185         i++;
186     }
187     widget.border->setItemsBackground(QColor(Qt::white));
188 
189     // widget.borderType->addItem("None");
190 
191     widget.border->addSeparator();
192 
193     /*
194         //TODO: Uncomment the below line when the string freeze is over
195         //action->setToolTip(i18n("Change the level the list is at"));
196 
197         QMenu *listLevelMenu = new QMenu();
198         const int levelIndent = 13;
199         for (int level = 0; level < 10; ++level) {
200             QWidgetAction *wa = new QWidgetAction(listLevelMenu);
201             ListLevelChooser *chooserWidget = new ListLevelChooser((levelIndent * level) + 5);
202             wa->setDefaultWidget(chooserWidget);
203             listLevelMenu->addAction(wa);
204             m_mapper->setMapping(wa,level + 1);
205             connect(chooserWidget, SIGNAL(clicked()), wa, SLOT(trigger()));
206             connect(wa, SIGNAL(triggered()), m_mapper, SLOT(map()));
207         }
208 
209         action->setMenu(listLevelMenu);
210         */
211 }
212