historywindow.cpp
author insilmaril
Mon, 05 Jan 2009 16:31:38 +0000
changeset 732 b77b56f707f1
parent 644 954221e01e4e
child 847 43268373032d
permissions -rw-r--r--
deleting works - partially.
     1 #include "historywindow.h"
     2 #include "mainwindow.h"
     3 
     4 
     5 extern QString iconPath;
     6 extern Settings settings;
     7 extern Main *mainWindow;
     8 
     9 HistoryWindow::HistoryWindow (QWidget *parent):QDialog (parent)
    10 {
    11 	ui.setupUi (this);
    12 	ui.historyTable->setRowCount (settings.value( "/mapeditor/stepsTotal",75).toInt());
    13 	ui.historyTable->setColumnCount (3);
    14 
    15 	
    16 	QTableWidgetItem *item;
    17 
    18 	item= new QTableWidgetItem(tr("Action","Table with actions"));
    19 	ui.historyTable->setHorizontalHeaderItem(0, item);
    20 	
    21 	item= new QTableWidgetItem(tr("Comment","Table with actions"));
    22 	ui.historyTable->setHorizontalHeaderItem(1, item);
    23 	
    24 	item= new QTableWidgetItem(tr("Undo action","Table with actions"));
    25 	ui.historyTable->setHorizontalHeaderItem(2, item);
    26 
    27 	ui.historyTable->setSelectionBehavior (QAbstractItemView::SelectRows);
    28 
    29 	ui.undoButton->setIcon (QIcon(iconPath+"/undo.png"));
    30 	ui.redoButton->setIcon (QIcon(iconPath+"/redo.png"));
    31 
    32 	connect ( ui.undoButton, SIGNAL (clicked()), this, SLOT (undo()));
    33 	connect ( ui.redoButton, SIGNAL (clicked()), this, SLOT (redo()));
    34 	connect ( ui.historyTable, SIGNAL (itemSelectionChanged()), this, SLOT (select()));
    35 
    36 	// Load Settings
    37 
    38 	resize (settings.value ( "/satellite/historywindow/geometry/size", QSize(1000,400)).toSize());
    39 	move   (settings.value ( "/satellite/historywindow/geometry/pos", QPoint (0,450)).toPoint());
    40 
    41 	ui.historyTable->setColumnWidth (0,settings.value("/satellite/historywindow/geometry/columnWidth/0",250).toInt());
    42 	ui.historyTable->setColumnWidth (1,settings.value("/satellite/historywindow/geometry/columnWidth/1",350).toInt());
    43 	ui.historyTable->setColumnWidth (2,settings.value("/satellite/historywindow/geometry/columnWidth/2",250).toInt());
    44 }
    45 
    46 HistoryWindow::~HistoryWindow()
    47 {
    48 	// Save settings
    49 	settings.setValue( "/satellite/historywindow/geometry/size", size() );
    50 	settings.setValue( "/satellite/historywindow/geometry/pos", pos() );
    51 
    52 	for (int i=0; i<3; ++i)
    53 		settings.setValue( QString("/satellite/historywindow/geometry/columnWidth/%1").arg(i), ui.historyTable->columnWidth (i) );
    54 }
    55 
    56 void HistoryWindow::clearRow(int row)
    57 {
    58 	QTableWidgetItem *it;
    59 	it=ui.historyTable->item (row,0);
    60 	if (it) it->setText ("");
    61 	it=ui.historyTable->item (row,1);
    62 	if (it) it->setText ("");
    63 	it=ui.historyTable->item (row,2);
    64 	if (it) it->setText ("");
    65 }
    66 
    67 void HistoryWindow::updateRow(int row, int step, SimpleSettings &set)
    68 {
    69 	QTableWidgetItem *item;
    70 
    71 	item= new QTableWidgetItem(set.readEntry(QString("/history/step-%1/redoCommand").arg(step)));
    72 	ui.historyTable->setItem(row, 0, item);
    73 
    74 	item= new QTableWidgetItem(set.readEntry(QString("/history/step-%1/comment").arg(step)));
    75 	ui.historyTable->setItem(row, 1, item);
    76 
    77 	item=new QTableWidgetItem(set.readEntry(QString("/history/step-%1/undoCommand").arg(step)));
    78 	ui.historyTable->setItem(row, 2, item);
    79 }
    80 
    81 void HistoryWindow::update(SimpleSettings &set)
    82 {
    83 	int undosAvail=set.readNumEntry("/history/undosAvail",0);
    84 	int redosAvail=set.readNumEntry("/history/redosAvail",0);
    85 	int stepsTotal=set.readNumEntry("/history/stepsTotal",0);
    86 	int curStep=set.readNumEntry ("/history/curStep");
    87 	int i;
    88 	int s=curStep;
    89 	int r=undosAvail-1;
    90 	QTableWidgetItem *item;
    91 
    92 	// Update number of rows
    93 	ui.historyTable->setRowCount (undosAvail + redosAvail +1);
    94 
    95 	// Update buttons
    96 	if (undosAvail<1)
    97 		ui.undoButton->setEnabled (false);
    98 	else	
    99 		ui.undoButton->setEnabled (true);
   100 
   101 	if (redosAvail<1)
   102 		ui.redoButton->setEnabled (false);
   103 	else	
   104 		ui.redoButton->setEnabled (true);
   105 
   106 	// Update undos in table
   107 	for (i=undosAvail; i>0; i--)
   108 	{
   109 		updateRow (r,s,set);
   110 		r--;
   111 		s--;
   112 		if (s<1) s=stepsTotal;
   113 	}
   114 	
   115 	// Generated the "now" row
   116 	QColor c(255,200,120);
   117 	for (i=0;i<=2;i++)
   118 	{
   119 		if (i!=1)
   120 		{
   121 			item=new QTableWidgetItem("");
   122 			item->setBackgroundColor (c);
   123 			ui.historyTable->setItem(undosAvail, i, item);
   124 		}
   125 	}
   126 	item=new QTableWidgetItem(" - " +tr("Current state","Current bar in history hwindow")+ " - ");
   127 	item->setBackgroundColor (c);
   128 	ui.historyTable->setItem(undosAvail, 1, item);
   129 
   130 	// Show "now" row
   131 	ui.historyTable->scrollToItem (item);
   132 
   133 	// Update Redos in table
   134 	s=curStep;
   135 	s++; if (s>stepsTotal) s=1;
   136 	for (i=1;i<= redosAvail; i++)
   137 	{
   138 		updateRow (undosAvail+i,s,set);
   139 		s++; if (s>stepsTotal) s=1;
   140 	}
   141 
   142 	// Delete the rest
   143 	for (i=undosAvail+redosAvail+1;i<= stepsTotal; i++)
   144 		clearRow (i);
   145 
   146 	//ui.historyTable->resizeColumnsToContents();
   147 }
   148 
   149 void HistoryWindow::setStepsTotal (int st)
   150 {
   151 	// Number of steps + "current" bar
   152 	ui.historyTable->setRowCount (st+1);
   153 }
   154 
   155 
   156 void HistoryWindow::closeEvent (QCloseEvent *)
   157 {
   158 	emit (windowClosed() );
   159 }
   160 
   161 void HistoryWindow::undo()
   162 {
   163 	mainWindow->editUndo();
   164 }
   165 
   166 void HistoryWindow::redo()
   167 {
   168 	mainWindow->editRedo();
   169 }
   170 
   171 void HistoryWindow::select()
   172 {
   173 	mainWindow->gotoHistoryStep (ui.historyTable->row (ui.historyTable->selectedItems().first()));
   174 }