historywindow.cpp
author insilmaril
Mon Oct 16 12:42:54 2006 +0000 (2006-10-16)
changeset 391 59106708b5b5
parent 381 c79df732d095
child 393 053b8645e3e9
permissions -rw-r--r--
Code simplifications
     1 #include "historywindow.h"
     2 #include "mapeditor.h"
     3 
     4 HistoryWindow::HistoryWindow (QWidget *parent):QDialog (parent)
     5 {
     6 	ui.setupUi (this);
     7 	ui.historyTable->setRowCount (20);
     8 	ui.historyTable->setColumnCount (3);
     9 
    10 	
    11 	QTableWidgetItem *item;
    12 
    13 	item= new QTableWidgetItem(tr("Action","Table with actions"));
    14 	ui.historyTable->setHorizontalHeaderItem(0, item);
    15 	
    16 	item= new QTableWidgetItem(tr("Comment","Table with actions"));
    17 	ui.historyTable->setHorizontalHeaderItem(1, item);
    18 	
    19 	item= new QTableWidgetItem(tr("Undo action","Table with actions"));
    20 	ui.historyTable->setHorizontalHeaderItem(2, item);
    21 
    22 	ui.historyTable->setSelectionBehavior (QAbstractItemView::SelectRows);
    23 
    24 	connect ( ui.undoButton, SIGNAL (clicked()), this, SLOT (undo()));
    25 	connect ( ui.redoButton, SIGNAL (clicked()), this, SLOT (redo()));
    26 	connect ( ui.historyTable, SIGNAL (itemSelectionChanged()), this, SLOT (select()));
    27 }
    28 
    29 
    30 void HistoryWindow::clearRow(int row)
    31 {
    32 	QTableWidgetItem *it;
    33 	it=ui.historyTable->item (row,0);
    34 	if (it) it->setText ("");
    35 	it=ui.historyTable->item (row,1);
    36 	if (it) it->setText ("");
    37 	it=ui.historyTable->item (row,2);
    38 	if (it) it->setText ("");
    39 }
    40 
    41 void HistoryWindow::updateRow(int row, int step, SimpleSettings &set)
    42 {
    43 	QTableWidgetItem *item;
    44 
    45 	item= new QTableWidgetItem(set.readEntry(QString("/history/step-%1/redoCommand").arg(step)));
    46 	ui.historyTable->setItem(row, 0, item);
    47 
    48 	item= new QTableWidgetItem(set.readEntry(QString("/history/step-%1/comment").arg(step)));
    49 	ui.historyTable->setItem(row, 1, item);
    50 
    51 	item=new QTableWidgetItem(set.readEntry(QString("/history/step-%1/undoCommand").arg(step)));
    52 	ui.historyTable->setItem(row, 2, item);
    53 }
    54 
    55 void HistoryWindow::update(SimpleSettings &set)
    56 {
    57 	int undosAvail=set.readNumEntry("/history/undosAvail",0);
    58 	int redosAvail=set.readNumEntry("/history/redosAvail",0);
    59 	int stepsTotal=set.readNumEntry("/history/stepsTotal",0);
    60 	int curStep=set.readNumEntry ("/history/curStep");
    61 	int i;
    62 	int s=curStep;
    63 	int r=undosAvail-1;
    64 	QTableWidgetItem *item;
    65 
    66 	// Update buttons
    67 	if (undosAvail<1)
    68 		ui.undoButton->setEnabled (false);
    69 	else	
    70 		ui.undoButton->setEnabled (true);
    71 
    72 	if (redosAvail<1)
    73 		ui.redoButton->setEnabled (false);
    74 	else	
    75 		ui.redoButton->setEnabled (true);
    76 
    77 	// Update undos in table
    78 	for (i=undosAvail; i>0; i--)
    79 	{
    80 		updateRow (r,s,set);
    81 		r--;
    82 		s--;
    83 		if (s<1) s=stepsTotal;
    84 	}
    85 	
    86 	// Generated the "now" row
    87 	QColor c(255,200,120);
    88 	for (i=0;i<=2;i++)
    89 	{
    90 		if (i!=1)
    91 		{
    92 			item=new QTableWidgetItem("");
    93 			item->setBackgroundColor (c);
    94 			ui.historyTable->setItem(undosAvail, i, item);
    95 		}
    96 	}
    97 	item=new QTableWidgetItem(" - " +tr("Current state","Current bar in history hwindow")+ " - ");
    98 	item->setBackgroundColor (c);
    99 	ui.historyTable->setItem(undosAvail, 1, item);
   100 
   101 
   102 	// Update Redos in table
   103 	s=curStep;
   104 	s++; if (s>stepsTotal) s=1;
   105 	for (i=1;i<= redosAvail; i++)
   106 	{
   107 		updateRow (undosAvail+i,s,set);
   108 		s++; if (s>stepsTotal) s=1;
   109 	}
   110 
   111 	// Delete the rest
   112 	for (i=undosAvail+redosAvail+1;i<= stepsTotal; i++)
   113 		clearRow (i);
   114 
   115 	ui.historyTable->resizeColumnsToContents();
   116 }
   117 
   118 void HistoryWindow::setME (MapEditor *me)
   119 {
   120 	mapEditor=me;
   121 }
   122 
   123 void HistoryWindow::setStepsTotal (int st)
   124 {
   125 	// Number of steps + "current" bar
   126 	ui.historyTable->setRowCount (st+1);
   127 
   128 }
   129 
   130 void HistoryWindow::undo()
   131 {
   132 	mapEditor->undo();
   133 }
   134 
   135 void HistoryWindow::redo()
   136 {
   137 	mapEditor->redo();
   138 }
   139 
   140 void HistoryWindow::select()
   141 {
   142 	mapEditor->gotoStep (ui.historyTable->row (ui.historyTable->selectedItems().first()));
   143 }