1 /***************************************************************************
2  *   copyright       : (C) 2003-2017 by Pascal Brachet                     *
3  *   http://www.xm1math.net/texmaker/                                      *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  ***************************************************************************/
11 
12 #include "tabdialog.h"
13 #include <QDebug>
14 
TabDialog(QWidget * parent,const char * name)15 TabDialog::TabDialog(QWidget *parent, const char *name)
16     :QDialog( parent)
17 {
18 setWindowTitle(name);
19 setModal(true);
20 ui.setupUi(this);
21 
22 colData col;
23 col.alignment=0;
24 col.leftborder=0;
25 liData li;
26 li.topborder=true;
27 li.merge=false;
28 li.mergefrom=1;
29 li.mergeto=1;
30 colDataList.clear();
31 liDataList.clear();
32 for ( int j = 0; j < 99; ++j )
33 {
34 colDataList.append(col);
35 liDataList.append(li);
36 }
37 ui.tableWidget->setRowCount( 2 );
38 ui.tableWidget->setColumnCount( 2 );
39 
40 ui.spinBoxRows->setValue(2);
41 ui.spinBoxRows->setRange(1,99);
42 connect( ui.spinBoxRows, SIGNAL(valueChanged(int)),this, SLOT(NewRows(int)));
43 
44 
45 ui.spinBoxColumns->setValue(2);
46 ui.spinBoxColumns->setRange(1,99);
47 connect( ui.spinBoxColumns, SIGNAL(valueChanged(int)),this, SLOT(NewColumns(int)));
48 
49 ui.spinBoxNumCol->setRange(1,2);
50 ui.spinBoxNumLi->setRange(1,2);
51 ui.spinBoxSpanFrom->setRange(1,1);
52 ui.spinBoxSpanTo->setRange(2,2);
53 
54 ui.comboBoxColAl->insertItem(0,"Center" );
55 ui.comboBoxColAl->insertItem(1,"Left" );
56 ui.comboBoxColAl->insertItem(2,"Right" );
57 ui.comboBoxColAl->insertItem(3, "p{} (fixed width - left)");
58 ui.comboBoxColAl->insertItem(4, "p{} (fixed width - center)");
59 ui.comboBoxColAl->insertItem(5, "p{} (fixed width - right)");
60 ui.comboBoxColAl->setCurrentIndex(0);
61 
62 ui.comboLeftBorder->insertItem(0,"|");
63 ui.comboLeftBorder->insertItem(1,"||");
64 ui.comboLeftBorder->insertItem(2,"none");
65 ui.comboLeftBorder->insertItem(3, "@{text}" );
66 ui.comboLeftBorder->setCurrentIndex(0);
67 
68 ui.comboBoxEndBorder->insertItem(0,"|");
69 ui.comboBoxEndBorder->insertItem(1,"||");
70 ui.comboBoxEndBorder->insertItem(2,"none");
71 ui.comboBoxEndBorder->insertItem(3, "@{text}" );
72 ui.comboLeftBorder->setCurrentIndex(0);
73 
74 ui.spinBoxNumCol->setValue(1);
75 ui.spinBoxNumLi->setValue(1);
76 
77 ui.checkBoxBorderTop->setChecked(true);
78 ui.checkBoxSpan->setChecked(false);
79 ui.spinBoxSpanFrom->setValue(1);
80 ui.spinBoxSpanTo->setValue(1);
81 ui.spinBoxSpanFrom->setEnabled(false);
82 ui.spinBoxSpanTo->setEnabled(false);
83 ui.checkBoxBorderBottom->setChecked(true);
84 ui.checkBoxMargin->setChecked(false);
85 
86 connect(ui.checkBoxSpan, SIGNAL(toggled(bool)),this, SLOT(updateSpanStatus(bool)));
87 
88 connect(ui.pushButtonColumns, SIGNAL(clicked()), this, SLOT(applytoAllColumns()));
89 connect(ui.pushButtonLines, SIGNAL(clicked()), this, SLOT(applytoAllLines()));
90 
91 connect( ui.spinBoxNumCol, SIGNAL(valueChanged(int)),this, SLOT(showColSettings(int)));
92 connect( ui.spinBoxNumLi, SIGNAL(valueChanged(int)),this, SLOT(showRowSettings(int)));
93 
94 connect(ui.comboBoxColAl, SIGNAL(currentIndexChanged(int)),this, SLOT(updateColSettings()));
95 connect(ui.comboLeftBorder, SIGNAL(currentIndexChanged(int)),this, SLOT(updateColSettings()));
96 
97 connect(ui.checkBoxBorderTop, SIGNAL(toggled(bool)),this, SLOT(updateRowSettings()));
98 connect(ui.checkBoxSpan, SIGNAL(toggled(bool)),this, SLOT(updateRowSettings()));
99 connect(ui.spinBoxSpanFrom, SIGNAL(valueChanged(int)),this, SLOT(updateRowSettings()));
100 connect(ui.spinBoxSpanTo, SIGNAL(valueChanged(int)),this, SLOT(updateRowSettings()));
101 
102 connect(ui.comboBoxEndBorder, SIGNAL(currentIndexChanged(int)),this, SLOT(updateTableWidget()));
103 
104 connect(ui.tableWidget, SIGNAL(cellClicked(int,int)),this, SLOT(showColRowSettings(int,int)));
105 connect(ui.tableWidget, SIGNAL(currentCellChanged(int,int,int,int)),this, SLOT(showColRowSettings(int,int)));
106 
107 setWindowTitle(tr("Quick Tabular"));
108 updateTableWidget();
109 }
110 
~TabDialog()111 TabDialog::~TabDialog(){
112 }
NewRows(int num)113 void TabDialog::NewRows(int num)
114 {
115 ui.tableWidget->setRowCount( num );
116 ui.spinBoxNumLi->setRange(1,num);
117 updateTableWidget();
118 }
119 
NewColumns(int num)120 void TabDialog::NewColumns(int num)
121 {
122 ui.tableWidget->setColumnCount( num );
123 ui.spinBoxNumCol->setRange(1,num);
124 if (num>1)
125   {
126   ui.spinBoxSpanFrom->setRange(1,num-1);
127   ui.spinBoxSpanTo->setRange(2,num);
128   }
129 else
130   {
131   ui.spinBoxSpanFrom->setRange(1,num-1);
132   ui.spinBoxSpanTo->setRange(2,num);
133   }
134 updateTableWidget();
135 }
136 
updateSpanStatus(bool enabled)137 void TabDialog::updateSpanStatus(bool enabled)
138 {
139 ui.spinBoxSpanFrom->setEnabled(enabled);
140 ui.spinBoxSpanTo->setEnabled(enabled);
141 updateTableWidget();
142 }
143 
applytoAllColumns()144 void TabDialog::applytoAllColumns()
145 {
146 colData col;
147 col.alignment=ui.comboBoxColAl->currentIndex();
148 col.leftborder=ui.comboLeftBorder->currentIndex();
149 for ( int i = 0; i < 99; ++i )
150   {
151   colDataList.replace(i,col);
152   }
153 updateTableWidget();
154 }
155 
applytoAllLines()156 void TabDialog::applytoAllLines()
157 {
158 liData li;
159 li.topborder=ui.checkBoxBorderTop->isChecked();
160 li.merge=ui.checkBoxSpan->isChecked();
161 li.mergefrom=ui.spinBoxSpanFrom->value();
162 li.mergeto=ui.spinBoxSpanTo->value();
163 if (li.mergefrom>li.mergeto)
164   {
165   li.mergefrom=1;
166   li.mergeto=1;
167   }
168 for ( int i = 0; i < 99; ++i )
169   {
170   liDataList.replace(i,li);
171   }
172 updateTableWidget();
173 }
174 
updateColSettings()175 void TabDialog::updateColSettings()
176 {
177 int i=ui.spinBoxNumCol->value()-1;
178 colData col;
179 col.alignment=ui.comboBoxColAl->currentIndex();
180 col.leftborder=ui.comboLeftBorder->currentIndex();
181 colDataList.replace(i,col);
182 updateTableWidget();
183 //qDebug() << "change" << i << colDataList.at(i).alignment << colDataList.at(i).leftborder;
184 }
185 
updateRowSettings()186 void TabDialog::updateRowSettings()
187 {
188 int i=ui.spinBoxNumLi->value()-1;
189 liData li;
190 li.topborder=ui.checkBoxBorderTop->isChecked();
191 li.merge=ui.checkBoxSpan->isChecked();
192 li.mergefrom=ui.spinBoxSpanFrom->value();
193 li.mergeto=ui.spinBoxSpanTo->value();
194 if (li.mergefrom>li.mergeto)
195   {
196   li.mergefrom=1;
197   li.mergeto=1;
198   }
199 liDataList.replace(i,li);
200 updateTableWidget();
201 }
202 
showColSettings(int column)203 void TabDialog::showColSettings(int column)
204 {
205 int i=column-1;
206 if (i>=99) return;
207 disconnect(ui.comboBoxColAl, SIGNAL(currentIndexChanged(int)),this, SLOT(updateColSettings()));
208 disconnect(ui.comboLeftBorder, SIGNAL(currentIndexChanged(int)),this, SLOT(updateColSettings()));
209 ui.comboBoxColAl->setCurrentIndex(colDataList.at(i).alignment);
210 ui.comboLeftBorder->setCurrentIndex(colDataList.at(i).leftborder);
211 connect(ui.comboBoxColAl, SIGNAL(currentIndexChanged(int)),this, SLOT(updateColSettings()));
212 connect(ui.comboLeftBorder, SIGNAL(currentIndexChanged(int)),this, SLOT(updateColSettings()));
213 updateTableWidget();
214 }
215 
showRowSettings(int row)216 void TabDialog::showRowSettings(int row)
217 {
218 int i=row-1;
219 if (i>=99) return;
220 disconnect(ui.checkBoxBorderTop, SIGNAL(toggled(bool)),this, SLOT(updateRowSettings()));
221 disconnect(ui.checkBoxSpan, SIGNAL(toggled(bool)),this, SLOT(updateRowSettings()));
222 disconnect(ui.spinBoxSpanFrom, SIGNAL(valueChanged(int)),this, SLOT(updateRowSettings()));
223 disconnect(ui.spinBoxSpanTo, SIGNAL(valueChanged(int)),this, SLOT(updateRowSettings()));
224 ui.checkBoxBorderTop->setChecked(liDataList.at(i).topborder);
225 ui.checkBoxSpan->setChecked(liDataList.at(i).merge);
226 ui.spinBoxSpanFrom->setValue(liDataList.at(i).mergefrom);
227 ui.spinBoxSpanTo->setValue(liDataList.at(i).mergeto);
228 connect(ui.checkBoxBorderTop, SIGNAL(toggled(bool)),this, SLOT(updateRowSettings()));
229 connect(ui.checkBoxSpan, SIGNAL(toggled(bool)),this, SLOT(updateRowSettings()));
230 connect(ui.spinBoxSpanFrom, SIGNAL(valueChanged(int)),this, SLOT(updateRowSettings()));
231 connect(ui.spinBoxSpanTo, SIGNAL(valueChanged(int)),this, SLOT(updateRowSettings()));
232 updateTableWidget();
233 }
234 
showColRowSettings(int row,int column)235 void TabDialog::showColRowSettings(int row,int column)
236 {
237 ui.spinBoxNumLi->setValue(row+1);
238 ui.spinBoxNumCol->setValue(column+1);
239 }
240 
updateTableWidget()241 void TabDialog::updateTableWidget()
242 {
243 QStringList borderlist, alignlist;
244 borderlist<< QString("|") << QString("||") << QString("") << QString("@{}");
245 alignlist << QString("c") << QString("l") << QString("r") << QString("l p{}") << QString("c p{}") << QString("r p{}");
246 int y = ui.spinBoxRows->value();
247 int x = ui.spinBoxColumns->value();
248 QStringList headerList;
249 QString tag="";
250 for ( int j=0;j<x;j++)
251   {
252   tag=borderlist.at(colDataList.at(j).leftborder);
253   tag+=alignlist.at(colDataList.at(j).alignment);
254   if (j<x-1) headerList.append(tag);
255   }
256 tag+=borderlist.at(ui.comboBoxEndBorder->currentIndex());
257 headerList.append(tag);
258 ui.tableWidget->setHorizontalHeaderLabels(headerList);
259 /*QColor selBlendColor=QColor("#0000FF");
260 QColor spancolor = selBlendColor.dark( 140 );
261 spancolor.setAlphaF( 0.2 );*/
262 QTableWidgetItem *item, *new_item;
263 QString content;
264 for ( int i=0;i<y;i++)
265   {
266   for ( int j=0;j<x;j++)
267     {
268     item=ui.tableWidget->item(i,j);
269     if (item)
270 	{
271 	content=item->text();
272 	new_item=new QTableWidgetItem();
273 	new_item->setText(content);
274 	}
275     else
276 	{
277 	new_item=new QTableWidgetItem();
278 	new_item->setText("");
279 	}
280     if (alignlist.at(colDataList.at(j).alignment).contains("l")) new_item->setTextAlignment(Qt::AlignLeft|Qt::AlignVCenter);
281     else if (alignlist.at(colDataList.at(j).alignment).contains("r")) new_item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
282     else new_item->setTextAlignment(Qt::AlignCenter);
283     if (ui.tableWidget->columnSpan(i,j)>1) ui.tableWidget->setSpan(i,j,1,1);
284 /*    if ((liDataList.at(i).merge) && (liDataList.at(i).mergeto>liDataList.at(i).mergefrom)&& (j>=liDataList.at(i).mergefrom-1) && (j<=liDataList.at(i).mergeto-1))
285       {
286       new_item->setBackgroundColor(spancolor);
287       }*/
288     ui.tableWidget->setItem(i,j,new_item);
289     }
290     if ((liDataList.at(i).merge) && (liDataList.at(i).mergeto>liDataList.at(i).mergefrom))
291       {
292       ui.tableWidget->setSpan(i,liDataList.at(i).mergefrom-1,1,liDataList.at(i).mergeto-liDataList.at(i).mergefrom+1);
293       }
294   }
295 }
296