branchpropwindow.cpp
author insilmaril
Mon, 05 May 2008 13:46:42 +0000
changeset 691 058b0e70f5ec
parent 686 d6cc3365c8b8
child 721 12958f987bcf
permissions -rw-r--r--
Some code cleanup and experimental stuff to support animations later
     1 #include "branchpropwindow.h"
     2 
     3 #include <QColorDialog>
     4 
     5 #include "frameobj.h"
     6 #include "settings.h"
     7 
     8 extern Settings settings;
     9 extern QString vymName;
    10 
    11 
    12 BranchPropertyWindow::BranchPropertyWindow (QWidget *parent): QDialog (parent)
    13 {
    14 	ui.setupUi (this);
    15 
    16 	setCaption(vymName +" - " +tr ("Property Editor","Window caption"));
    17 
    18 	branch=NULL;
    19 	mapEditor=NULL;
    20 
    21 	ui.tabWidget->setEnabled(false);
    22 
    23 	penColor=QColor (Qt::black);
    24 	brushColor=QColor (Qt::black);
    25     QPixmap pix( 16,16);
    26     pix.fill (penColor);
    27 	ui.framePenColorButton->setPixmap (pix);
    28 	ui.frameBrushColorButton->setPixmap (pix);
    29 
    30 	// Create Model and View to hold attributes
    31 	attributeModel = new QStandardItemModel (1,3,this);
    32 	attributeModel->setHeaderData(0, Qt::Horizontal, tr("Name","Branchprop window: Attribute name"));
    33 	attributeModel->setHeaderData(1, Qt::Horizontal, tr("Value","Branchprop window: Attribute value"));
    34 	attributeModel->setHeaderData(2, Qt::Horizontal, tr("Type","Branchprop window: Attribute type"));
    35 	ui.attributeTableView->setModel (attributeModel);
    36 	
    37 
    38 	// Load Settings
    39 	resize (settings.value ( "/satellite/propertywindow/geometry/size", QSize(450,600)).toSize());
    40 	move   (settings.value ( "/satellite/propertywindow/geometry/pos", QPoint (250,50)).toPoint());
    41 	
    42 	if (settings.value ( "/satellite/propertywindow/showWithMain",true).toBool())
    43 		show();
    44 	else	
    45 		hide();
    46 
    47 	// FIXME for now remove attribute tab
    48 	ui.tabWidget->removeTab (3);
    49 
    50 }
    51 
    52 BranchPropertyWindow::~BranchPropertyWindow ()
    53 {
    54 	settings.setValue( "/satellite/propertywindow/geometry/size", size() );
    55 	settings.setValue( "/satellite/propertywindow/geometry/pos", pos() );
    56 	settings.setValue( "/satellite/propertywindow/showWithMain",isVisible() );
    57 }
    58 
    59 void BranchPropertyWindow::setBranch (BranchObj *bo)
    60 {
    61 	disconnectSignals();
    62 	branch=bo;
    63 	if (bo) 
    64 	{
    65 		ui.tabWidget->setEnabled (true);
    66 
    67 		// Frame
    68 		FrameObj::FrameType t=branch->getFrameType();
    69 		if (t==FrameObj::NoFrame)
    70 		{
    71 			ui.frameTypeCombo->setCurrentIndex (0);
    72 			penColor=Qt::white;
    73 			brushColor=Qt::white;
    74 			ui.colorGroupBox->setEnabled (false);
    75 			ui.framePaddingSpinBox->setEnabled (false);
    76 			ui.frameWidthSpinBox->setEnabled (false);
    77 			ui.framePaddingLabel->setEnabled (false);
    78 			ui.frameBorderLabel->setEnabled (false);
    79 		} else	
    80 		{
    81 			penColor=bo->getFramePenColor();
    82 			brushColor=bo->getFrameBrushColor();
    83 			QPixmap pix( 16,16);
    84 			pix.fill (penColor);
    85 			ui.framePenColorButton->setPixmap (pix);
    86 			pix.fill (brushColor);
    87 			ui.frameBrushColorButton->setPixmap (pix);
    88 			ui.colorGroupBox->setEnabled (true);
    89 			ui.framePaddingSpinBox->setEnabled (true);
    90 			ui.framePaddingSpinBox->setValue (bo->getFramePadding());
    91 			ui.frameWidthSpinBox->setEnabled (true);
    92 			ui.frameWidthSpinBox->setValue (bo->getFrameBorderWidth());
    93 			ui.framePaddingLabel->setEnabled (true);
    94 			ui.frameBorderLabel->setEnabled (true);
    95 
    96 			switch (t)
    97 			{
    98 				case FrameObj::Rectangle: 
    99 					ui.frameTypeCombo->setCurrentIndex (1);
   100 					break;
   101 				case FrameObj::Ellipse: 
   102 					ui.frameTypeCombo->setCurrentIndex (2);
   103 					break;
   104 				default: 
   105 					break;
   106 			}
   107 		}	
   108 		
   109 		// Link
   110 		if (branch->getHideLinkUnselected())
   111 			ui.hideLinkIfUnselected->setCheckState (Qt::Checked);
   112 		else	
   113 			ui.hideLinkIfUnselected->setCheckState (Qt::Unchecked);
   114 
   115 		// Layout
   116 		if (branch->getIncludeImagesVer())
   117 			ui.incImgVer->setCheckState (Qt::Checked);
   118 		else	
   119 			ui.incImgVer->setCheckState (Qt::Unchecked);
   120 		if (branch->getIncludeImagesHor())
   121 			ui.incImgHor->setCheckState (Qt::Checked);
   122 		else	
   123 			ui.incImgHor->setCheckState (Qt::Unchecked);
   124 
   125 		// Attributes
   126 		attributeModel->removeRows(0, attributeModel->rowCount(), QModelIndex());
   127 
   128 		// FIXME some samples for testing
   129 		QStringList attrTypes=mapEditor->attributeTable()->getTypes();
   130 		for (int i=0; i<attrTypes.count()-1;i++)
   131 		{
   132 			attributeModel->insertRow (i,QModelIndex ());
   133 			attributeModel->setData(attributeModel->index(i, 0, QModelIndex()), QString ("Name %1").arg(i));
   134 			attributeModel->setData(attributeModel->index(i, 1, QModelIndex()), i);
   135 			attributeModel->setData(attributeModel->index(i, 2, QModelIndex()), attrTypes.at(i));
   136 		}
   137 
   138 
   139 		ui.attributeTableView->resizeColumnsToContents();
   140 
   141 		// Initialize Delegate
   142 		delegate.setAttributeTable (mapEditor->attributeTable());
   143 		ui.attributeTableView->setItemDelegate (&delegate);
   144 
   145 
   146 		// Finally activate signals
   147 		connectSignals();
   148 	} else
   149 	{
   150 		ui.tabWidget->setEnabled (false);
   151 	}
   152 }
   153 
   154 void BranchPropertyWindow::setMapEditor (MapEditor *me)
   155 {
   156 	mapEditor=me;
   157 	if (mapEditor) 
   158 		setBranch (mapEditor->getSelectedBranch() );
   159 	else
   160 		ui.tabWidget->setEnabled (false);
   161 		
   162 }
   163 
   164 void BranchPropertyWindow::frameTypeChanged (int i)
   165 {
   166 	if (mapEditor)
   167 	{
   168 		switch (i)
   169 		{
   170 			case 0: mapEditor->setFrameType (FrameObj::NoFrame); break;
   171 			case 1: 
   172 				mapEditor->setFrameType (FrameObj::Rectangle); 
   173 				break;
   174 			case 2: 
   175 				mapEditor->setFrameType (FrameObj::Ellipse); 
   176 				mapEditor->setFramePadding (5); 
   177 				break;
   178 		}
   179 		setBranch (branch);
   180 	}	
   181 }
   182 
   183 void BranchPropertyWindow::framePenColorClicked()
   184 {
   185 	if (mapEditor) 
   186 	{	
   187 		QColor col = QColorDialog::getColor( penColor, this );
   188 		if ( col.isValid() ) 
   189 		{
   190 			penColor=col;
   191 			mapEditor->setFramePenColor (penColor);
   192 		}	
   193 	}
   194 }
   195 
   196 void BranchPropertyWindow::frameBrushColorClicked()
   197 {
   198 	if (mapEditor) 
   199 	{
   200 		QColor col = QColorDialog::getColor( brushColor, this );
   201 		if ( col.isValid() ) 
   202 		{
   203 			brushColor=col;
   204 			mapEditor->setFrameBrushColor (brushColor);
   205 		}	
   206 	}	
   207 }
   208 
   209 void BranchPropertyWindow::framePaddingChanged(int i)
   210 {
   211 	if (mapEditor) mapEditor->setFramePadding (i);
   212 }
   213 
   214 void BranchPropertyWindow::frameBorderWidthChanged(int i)
   215 {
   216 	if (mapEditor) mapEditor->setFrameBorderWidth(i);
   217 }
   218 
   219 void BranchPropertyWindow::linkHideUnselectedChanged (int i)
   220 {
   221 	if (!branch) return;
   222 	mapEditor->setHideLinkUnselected(i);
   223 }
   224 
   225 void BranchPropertyWindow::incImgVerChanged (int  i)
   226 {
   227 	if (mapEditor) mapEditor->setIncludeImagesVer (i);
   228 }
   229 
   230 void BranchPropertyWindow::incImgHorChanged (int  i)
   231 {
   232 	if (mapEditor) mapEditor->setIncludeImagesHor (i);
   233 }
   234 
   235 void BranchPropertyWindow::closeEvent( QCloseEvent* ce )
   236 {
   237     ce->accept();	// can be reopened with show()
   238 	hide();
   239 	emit (windowClosed() );
   240     return;
   241 }
   242 
   243 void BranchPropertyWindow::addAttributeClicked()
   244 {
   245 	// Add empty line for adding attributes
   246 	attributeModel->insertRow (attributeModel->rowCount (),QModelIndex ());
   247 	attributeModel->setData(attributeModel->index(attributeModel->rowCount()-1, 0, QModelIndex()),  "Add new");
   248 	attributeModel->setData(attributeModel->index(attributeModel->rowCount()-1, 2, QModelIndex()),  "Undefined");
   249 
   250 	// Select attribute from list
   251 	ui.attributeTableView->edit (attributeModel->index(attributeModel->rowCount()-1,0, QModelIndex() ));
   252 	ui.attributeTableView->resizeColumnsToContents();
   253 
   254 //	QString attname=attributeModel->in
   255 //	attributeModel->setData(attributeModel->index(attributeModel->rowCount()-1, 2, QModelIndex()),  );
   256 
   257 
   258 
   259 	ui.attributeTableView->edit (attributeModel->index(attributeModel->rowCount()-1,1, QModelIndex() ));
   260 
   261 }
   262 
   263 void BranchPropertyWindow::deleteAttributeClicked()
   264 {
   265 	cout << "BPW::delete\n";
   266 }
   267 
   268 void BranchPropertyWindow::connectSignals()
   269 {
   270 	// Frame
   271 	connect ( 
   272 		ui.framePenColorButton, SIGNAL (clicked()), 
   273 		this, SLOT (framePenColorClicked()));
   274 	connect ( 
   275 		ui.framePaddingSpinBox, SIGNAL (valueChanged( int)), 
   276 		this, SLOT (framePaddingChanged (int)));
   277 	connect ( 
   278 		ui.frameWidthSpinBox, SIGNAL (valueChanged( int)), 
   279 		this, SLOT (frameBorderWidthChanged (int)));
   280 	connect ( 
   281 		ui.frameBrushColorButton, SIGNAL (clicked()), 
   282 		this, SLOT (frameBrushColorClicked()));
   283 	connect ( 
   284 		ui.frameTypeCombo, SIGNAL (currentIndexChanged( int)), 
   285 		this, SLOT (frameTypeChanged (int)));
   286 
   287 
   288 	// Link	
   289 	connect ( 
   290 		ui.hideLinkIfUnselected, SIGNAL (stateChanged( int)), 
   291 		this, SLOT (linkHideUnselectedChanged (int)));
   292 
   293 	// Layout	
   294 	connect ( 
   295 		ui.incImgVer, SIGNAL (stateChanged( int)), 
   296 		this, SLOT (incImgVerChanged (int)));
   297 	connect ( 
   298 		ui.incImgHor, SIGNAL (stateChanged( int)), 
   299 		this, SLOT (incImgHorChanged (int)));
   300 
   301 	// Attributes	
   302 	connect ( 
   303 		ui.addAttributeButton, SIGNAL (clicked()), 
   304 		this, SLOT (addAttributeClicked()));
   305 	connect ( 
   306 		ui.deleteAttributeButton, SIGNAL (clicked()), 
   307 		this, SLOT (deleteAttributeClicked()));
   308 }
   309 
   310 
   311 void BranchPropertyWindow::disconnectSignals()
   312 {
   313 	// Frame 
   314 	disconnect ( ui.frameTypeCombo, 0,0,0);
   315 	disconnect ( ui.framePenColorButton, 0,0,0);
   316 	disconnect ( ui.framePaddingSpinBox, 0,0,0);
   317 	disconnect ( ui.frameWidthSpinBox, 0,0,0);
   318 	disconnect ( ui.frameBrushColorButton, 0,0,0);
   319 
   320 	// Link	
   321 	disconnect ( ui.hideLinkIfUnselected, 0,0,0);
   322 
   323 	// Layout	
   324 	disconnect ( ui.incImgVer, 0,0,0);
   325 	disconnect ( ui.incImgHor, 0,0,0);
   326 
   327 	// Attributes
   328 	disconnect ( ui.addAttributeButton, 0,0,0);
   329 	disconnect ( ui.deleteAttributeButton, 0,0,0);
   330 }
   331 
   332