7#include <tqwhatsthis.h>
9#include <tqvalidator.h>
10#include <tqpushbutton.h>
12#include <kinputdialog.h>
15#include <tdemessagebox.h>
16#include <kiconloader.h>
19#include "kchart_params.h"
20#include "kchart_factory.h"
22#include "kchartDataEditor.h"
23#include "kchartDataEditor.moc"
36kchartDataSpinBox::kchartDataSpinBox(TQWidget *parent)
43kchartDataSpinBox::~kchartDataSpinBox()
48void kchartDataSpinBox::stepUp()
51 uint
const new_value = value() + 1;
56 emit valueChangedSpecial( value() );
61void kchartDataSpinBox::stepDown()
65 uint
const new_value = value() - 1;
66 TQSpinBox::stepDown();
69 emit valueChangedSpecial( value() );
74bool kchartDataSpinBox::eventFilter( TQObject *obj, TQEvent *ev )
76 if ( obj == editor() ) {
77 if ( ev->type() == TQEvent::FocusOut ) {
79 setValue(editor()->text().toInt());
84 emit valueChangedSpecial( value() );
89 return TQSpinBox::eventFilter( obj, ev );
99kchartDataTable::kchartDataTable(TQWidget *parent)
105kchartDataTable::~kchartDataTable()
110bool kchartDataTable::eventFilter( TQObject *obj, TQEvent *ev )
112 if (ev->type() == TQEvent::KeyPress && strcmp(obj->name(),
113 "qt_tableeditor")==0 ) {
114 TQKeyEvent *e = (TQKeyEvent *)ev;
116 switch ( e->key() ) {
119 if ( currentRow() > 0 ) {
120 setCurrentCell( currentRow()-1, currentColumn() );
121 editCell(currentRow(), currentColumn() );
128 if ( currentRow() < numRows()-1 ) {
129 setCurrentCell( currentRow()+1, currentColumn() );
130 editCell(currentRow(), currentColumn() );
137 if ( currentColumn() < numCols()-1 ) {
138 setCurrentCell( currentRow(), currentColumn()+1 );
139 editCell(currentRow(), currentColumn() );
146 if ( currentColumn() > 0 ) {
147 setCurrentCell( currentRow(), currentColumn()-1 );
148 editCell(currentRow(), currentColumn() );
156 return TQTable::eventFilter( obj, ev );
164#define COLUMNWIDTH 80
166kchartDataEditor::kchartDataEditor(TQWidget* parent) :
167 KDialogBase(parent,
"dataeditor", true, i18n(
"KChart Data Editor"),
168 KDialogBase::Ok | KDialogBase::Cancel | KDialogBase::Apply,
169 KDialogBase::Ok, true)
171 TQWidget *page =
new TQWidget(
this );
175 m_table =
new kchartDataTable(page);
176 m_table->setSelectionMode(TQTable::NoSelection);
178 m_table->setRowMovingEnabled(
true);
179 m_table->setColumnMovingEnabled(
true);
181 connect( m_table, TQ_SIGNAL( currentChanged(
int,
int) ),
182 this, TQ_SLOT( currentChanged(
int,
int) ) );
185 m_rowsLA =
new TQLabel( i18n(
"# Rows:" ), page );
186 m_rowsLA->resize( m_rowsLA->sizeHint() );
187 m_rowsSB =
new kchartDataSpinBox( page );
188 m_rowsSB->resize( m_rowsSB->sizeHint() );
189 m_rowsSB->setMinValue(1);
192 m_colsLA =
new TQLabel( i18n(
"# Columns:" ), page );
193 m_colsLA->resize( m_colsLA->sizeHint() );
194 m_colsSB =
new kchartDataSpinBox( page );
195 m_colsSB->resize( m_colsSB->sizeHint() );
196 m_colsSB->setMinValue(1);
200 m_firstRowAsLabel =
new TQCheckBox( i18n(
"First row as label" ), page);
201 m_firstColAsLabel =
new TQCheckBox( i18n(
"First column as label" ), page);
206 m_insertRowButton =
new TQPushButton( page);
208 m_insertRowButton->setPixmap( BarIcon( TQString(
"insert_table_row"),
210 TDEIcon::DefaultState,
211 KChartFactory::global() ) );
213 connect( m_insertRowButton, TQ_SIGNAL( clicked() ),
214 this, TQ_SLOT( insertRow() ) );
216 m_removeRowButton =
new TQPushButton( page );
217 m_removeRowButton->setPixmap( BarIcon( TQString(
"delete_table_row"),
219 TDEIcon::DefaultState,
220 KChartFactory::global() ) );
222 connect( m_removeRowButton, TQ_SIGNAL( clicked() ),
223 this, TQ_SLOT( removeCurrentRow() ) );
225 m_insertColButton =
new TQPushButton( page );
226 m_insertColButton->setPixmap( BarIcon( TQString(
"insert_table_col"),
228 TDEIcon::DefaultState,
229 KChartFactory::global() ) );
231 connect( m_insertColButton, TQ_SIGNAL( clicked() ),
232 this, TQ_SLOT( insertColumn() ) );
234 m_removeColButton =
new TQPushButton( page );
235 m_removeColButton->setPixmap( BarIcon( TQString(
"delete_table_col"),
237 TDEIcon::DefaultState,
238 KChartFactory::global() ) );
240 connect( m_removeColButton, TQ_SIGNAL( clicked() ),
241 this, TQ_SLOT( removeCurrentColumn() ) );
244 TQVBoxLayout *topLayout =
new TQVBoxLayout( page );
246 TQHBoxLayout* insertRemoveLayout =
new TQHBoxLayout( );
248 insertRemoveLayout->setSpacing(5);
249 insertRemoveLayout->addWidget(m_insertRowButton);
250 insertRemoveLayout->addWidget(m_removeRowButton);
251 insertRemoveLayout->addWidget(m_insertColButton);
252 insertRemoveLayout->addWidget(m_removeColButton);
253 insertRemoveLayout->addStretch(1);
255 topLayout->addLayout(insertRemoveLayout);
256 topLayout->addSpacing(10);
259 topLayout->addWidget(m_table);
262 TQHBoxLayout *hbl1 =
new TQHBoxLayout( );
263 hbl1->addWidget(m_rowsLA);
264 hbl1->addWidget(m_rowsSB);
265 hbl1->addSpacing(20);
266 hbl1->addWidget(m_colsLA);
267 hbl1->addWidget(m_colsSB);
270 topLayout->addLayout(hbl1);
274 TQHBoxLayout *hbl2 =
new TQHBoxLayout( );
275 hbl2->addWidget(m_firstRowAsLabel);
276 hbl2->addWidget(m_firstColAsLabel);
279 topLayout->addLayout(hbl2);
282 topLayout->setStretchFactor(m_table, 1);
283 topLayout->setStretchFactor(insertRemoveLayout,1);
286 connect(m_rowsSB, TQ_SIGNAL(valueChangedSpecial(
int)),
287 this, TQ_SLOT(setRows(
int)));
288 connect(m_colsSB, TQ_SIGNAL(valueChangedSpecial(
int)),
289 this, TQ_SLOT(setCols(
int)));
296 connect(m_table->horizontalHeader(), TQ_SIGNAL(clicked(
int)),
297 this, TQ_SLOT(column_clicked(
int)) );
298 connect(m_table->verticalHeader(), TQ_SIGNAL(clicked(
int)),
299 this, TQ_SLOT(row_clicked(
int)) );
302 connect(m_table, TQ_SIGNAL(valueChanged(
int,
int)),
303 this, TQ_SLOT(tableChanged(
int,
int)) );
308 m_userWantsToShrink =
false;
315 m_removeRowButton->setEnabled(
false );
316 m_removeColButton->setEnabled(
false );
325void kchartDataEditor::addDocs()
328 TQString rowwhatsthis = i18n(
"<p><b>Sets the number of rows in the data table."
329 "</b><br><br>Each row represents one data set.</p>");
330 TQToolTip::add(m_rowsSB, i18n(
"Number of active data rows"));
331 TQWhatsThis::add(m_rowsLA, rowwhatsthis);
332 TQWhatsThis::add(m_rowsSB, rowwhatsthis);
335 TQString colwhatsthis = i18n(
"<p><b>Sets the number of columns in the data table."
336 "</b><br><br>The number of columns defines the number of data values in each data set (row).</p>");
337 TQToolTip::add(m_colsSB, i18n(
"Number of active data columns"));
338 TQWhatsThis::add(m_colsLA, colwhatsthis);
339 TQWhatsThis::add(m_colsSB, colwhatsthis);
342 TQToolTip::add(m_table, i18n(
"Chart data table."));
364 TQWhatsThis::add(m_table, i18n(
"<p>This table contains the data"
365 " for the chart.<br><br> Each row is one data set of values."
366 " The name of such a data set can be changed in the column header (on the left)"
367 " of the table. In a line diagram each row is one line. In a ring diagram each row"
368 " is one slice. <br><br> Each column represents one value of each data set."
369 " Just like rows you can also change the name of each value in the"
370 " column headers (at the top) of the table. In a bar diagram the number"
371 " of columns defines the number of value sets. In a ring diagram each"
372 " column is one ring.</p>"));
375 TQToolTip::add( m_insertRowButton, i18n(
"Insert row") );
376 TQToolTip::add( m_removeRowButton, i18n(
"Delete row") );
377 TQToolTip::add( m_insertColButton, i18n(
"Insert column") );
378 TQToolTip::add( m_removeColButton, i18n(
"Delete column") );
387void kchartDataEditor::setData( KChartParams *params, KDChartTableData *dat )
389 unsigned int rowsCount;
390 unsigned int colsCount;
393 if ( dat->usedRows() == 0 && dat->usedCols() == 0) {
394 rowsCount = dat->rows();
395 colsCount = dat->cols();
398 rowsCount = dat->usedRows();
399 colsCount = dat->usedCols();
403 if ( rowsCount==0 && colsCount==0 ) {
404 m_table->setNumRows(1);
405 m_table->setNumCols(1);
410 rowsCount += headerRows();
411 colsCount += headerCols();
414 m_rowsSB->setValue(rowsCount);
415 m_colsSB->setValue(colsCount);
417 m_firstRowAsLabel->setChecked( params->firstRowAsLabel() );
418 m_firstColAsLabel->setChecked( params->firstColAsLabel() );
422 m_table->setNumRows(rowsCount);
423 m_table->setNumCols(colsCount);
424 for (
unsigned int row = headerRows(); row < rowsCount; row++) {
425 for (
unsigned int col = headerCols(); col < colsCount; col++) {
426 TQVariant t = dat->cellVal(row-headerRows(), col-headerCols());
430 if ( t.type() == TQVariant::Double ) {
431 m_table->setText(row, col,
432 TQString(
"%1").arg(t.toDouble()));
434 else if ( t.type() == TQVariant::String )
435 kdDebug(35001) <<
"I cannot handle strings in the table yet"
445 for (
unsigned int col = 0; col < colsCount + 1; col++)
446 m_table->setColumnWidth(col, COLUMNWIDTH);
455void kchartDataEditor::getData( KChartParams *params, KDChartTableData *dat )
458 int labelRows = headerRows();
460 int labelCols = headerCols();
462 int numRows = m_table->numRows()-labelRows;
463 int numCols = m_table->numCols()-labelCols;
467 if (
static_cast<int>( dat->rows() ) < numRows
468 ||
static_cast<int>( dat->cols() ) < numCols )
469 dat->expand( numRows, numCols );
471 dat->setUsedRows( numRows );
472 dat->setUsedCols( numCols );
476 if ( numRows==1 && numCols==1 && m_table->horizontalHeader()->label(0).isEmpty()
477 && m_table->verticalHeader()->label(0).isEmpty()
478 && m_table->text(0, 0).isEmpty() ) {
485 for (
int row = labelRows ; row < (numRows+labelRows); row++) {
486 for (
int col = labelCols ; col < (numCols+labelCols); col++) {
489 TQString tmp = m_table->text(row, col);
491 double val = tmp.toDouble( &bOk );
496 dat->setCell(row-labelRows,col-labelCols, val);
501 params->setFirstRowAsLabel( m_firstRowAsLabel->isChecked() );
502 params->setFirstColAsLabel( m_firstColAsLabel->isChecked() );
509void kchartDataEditor::setRowLabels(
const TQStringList &rowLabels)
512 TQHeader *rowHeader = m_table->verticalHeader();
514 int numRows = m_rowsSB->value();
516 rowHeader->setLabel(0,
"");
517 if ( numRows==1 && m_colsSB->value()==1 && m_table->text(0, 0).isEmpty() )
519 for (row = 0; row < numRows; row++) {
520 rowHeader->setLabel(row, rowLabels[row]);
524 for (
unsigned int i=0; i < rowLabels.count(); i++) {
525 m_table->setText(i + headerRows(), 0, rowLabels[i]);
532int kchartDataEditor::headerRows()
537int kchartDataEditor::headerCols()
545void kchartDataEditor::getRowLabels(TQStringList &rowLabels)
548 TQHeader *rowHeader = m_table->verticalHeader();
549 int numRows = m_rowsSB->value();
553 for (row = 0; row < numRows; row++) {
554 rowLabels << rowHeader->label(row);
559 for (
int i=headerRows();i < m_table->numRows();i++)
561 rowLabels << m_table->text(i,0);
568void kchartDataEditor::setColLabels(
const TQStringList &colLabels)
571 TQHeader *colHeader = m_table->horizontalHeader();
574 int numCols = m_colsSB->value();
576 colHeader->setLabel(0,
"");
577 if ( m_rowsSB->value()==1 && numCols==1 && m_table->text(0, 0).isEmpty() )
579 for (col = 0; col < numCols; col++) {
580 colHeader->setLabel(col, colLabels[col]);
584 for (
unsigned int i = 0; i < colLabels.count(); i++)
586 m_table->setText(0,i+headerCols(),colLabels[i]);
595void kchartDataEditor::getColLabels(TQStringList &colLabels)
598 TQHeader *colHeader = m_table->horizontalHeader();
599 int numCols = m_colsSB->value();
603 for (col = 0; col < numCols; col++) {
604 colLabels << colHeader->label(col);
610 for (
int i = headerCols(); i < m_table->numCols(); i++) {
611 colLabels << m_table->text(0, i);
623void kchartDataEditor::removeCurrentRow()
625 int row = m_table->currentRow();
632 if ( m_table->numRows() == 2 )
635 m_table->removeRow(row);
636 m_rowsSB->setValue(m_table->numRows());
641void kchartDataEditor::removeCurrentColumn()
643 int col = m_table->currentColumn();
650 if ( m_table->numCols() == 2 )
653 m_table->removeColumn(col);
654 m_colsSB->setValue(m_table->numCols());
659void kchartDataEditor::insertColumn()
661 m_table->insertColumns(m_table->currentColumn() + 1, 1);
662 m_colsSB->setValue(m_table->numCols());
668void kchartDataEditor::insertRow()
670 m_table->insertRows(m_table->currentRow() + 1, 1);
671 m_rowsSB->setValue(m_table->numRows());
681static int askUserForConfirmation(TQWidget *parent)
683 return KMessageBox::warningContinueCancel(parent,
684 i18n(
"You are about to shrink the data table and remove some values. "
685 "This will lead to loss of existing data in the table "
686 "and/or the headers.\n\n"
687 "This message will not be shown again if you click Continue"));
693void kchartDataEditor::setRows(
int rows)
695 kdDebug(35001) <<
"setRows called: rows = " << rows << endl;;
700 m_rowsSB->setValue(1);
704 int old_rows = m_table->numRows();
705 if (rows > old_rows) {
706 m_table->setNumRows(rows);
709 for (
int i = old_rows; i < rows; i++)
710 m_table->verticalHeader()->setLabel(i,
"");
714 else if (rows < m_table->numRows()) {
715 bool ask_user =
false;
718 for (
int col=0; col<m_table->numCols(); col++) {
719 if (!m_table->text(rows, col).isEmpty()) {
726 if ( ask_user && !m_userWantsToShrink
727 && askUserForConfirmation(
this) == KMessageBox::Cancel) {
730 m_rowsSB->setValue(m_table->numRows());
736 m_userWantsToShrink =
true;
739 m_table->setNumRows(rows);
748void kchartDataEditor::setCols(
int cols)
750 kdDebug(35001) <<
"setCols called: cols = " << cols << endl;;
755 m_colsSB->setValue(1);
759 int old_cols = m_table->numCols();
760 if (cols > old_cols) {
761 m_table->setNumCols(cols);
764 for (
int i = old_cols; i < cols; i++) {
765 m_table->horizontalHeader()->setLabel(i,
"");
766 m_table->setColumnWidth(i, COLUMNWIDTH);
771 else if (cols < m_table->numCols()) {
772 bool ask_user =
false;
775 for (
int row=0; row<m_table->numRows(); row++) {
776 if (!m_table->text(row, cols).isEmpty()) {
783 if (ask_user && !m_userWantsToShrink
784 && askUserForConfirmation(
this) == KMessageBox::Cancel) {
787 m_colsSB->setValue(m_table->numCols());
793 m_userWantsToShrink =
true;
796 m_table->setNumCols(cols);
806void kchartDataEditor::column_clicked(
int column)
809 TQString name = KInputDialog::getText(i18n(
"Column Name"),
810 i18n(
"Type a new column name:"),
811 m_table->horizontalHeader()->label(column),
812 &ok,
this, 0,
new TQRegExpValidator(TQRegExp(
".*"),
this) );
816 m_table->horizontalHeader()->setLabel(column, name);
824void kchartDataEditor::row_clicked(
int row)
827 TQString name = KInputDialog::getText(i18n(
"Row Name"),
828 i18n(
"Type a new row name:"),
829 m_table->verticalHeader()->label(row),
830 &ok,
this, 0,
new TQRegExpValidator(TQRegExp(
".*"),
this) );
834 m_table->verticalHeader()->setLabel(row, name);
841void kchartDataEditor::tableChanged(
int row,
int col)
843 if (row <= headerRows())
845 if (col <= headerCols())
852void kchartDataEditor::currentChanged(
int row,
int col)
854 m_removeRowButton->setEnabled( row != 0 && m_table->numRows() > 2 );
855 m_removeColButton->setEnabled( col != 0 && m_table->numCols() > 2 );
859void kchartDataEditor::updateRowHeaders()
861 for (
int i=0;i<m_table->numRows();i++)
863 TQHeader* header=m_table->verticalHeader();
865 TQString tableCellText=m_table->text(i,0);
867 if (tableCellText == TQString())
868 tableCellText=TQString(
"");
870 header->setLabel(header->mapToSection(i),tableCellText);
874void kchartDataEditor::updateColHeaders()
876 for (
int i=0;i<m_table->numCols();i++)
878 TQHeader* header=m_table->horizontalHeader();
880 TQString tableCellText=m_table->text(0,i);
882 if (tableCellText == TQString())
883 tableCellText=TQString(
"");
885 header->setLabel(header->mapToSection(i),tableCellText);
894void kchartDataEditor::slotApply()
896 emit applyClicked(
this);
Provide access to the chart axis parameters.