1 /*******************************************************************
2 
3 Part of the Fritzing project - http://fritzing.org
4 Copyright (c) 2007-2014 Fachhochschule Potsdam - http://fh-potsdam.de
5 
6 Fritzing is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 Fritzing is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with Fritzing.  If not, see <http://www.gnu.org/licenses/>.
18 
19 ********************************************************************
20 
21 $Revision: 6904 $:
22 $Author: irascibl@gmail.com $:
23 $Date: 2013-02-26 16:26:03 +0100 (Di, 26. Feb 2013) $
24 
25 ********************************************************************/
26 
27 #include "layerpalette.h"
28 
29 #include <QAction>
30 
31 
ViewLayerCheckBox(QWidget * parent)32 ViewLayerCheckBox::ViewLayerCheckBox(QWidget * parent) : QCheckBox(parent) {
33 }
34 
35 
~ViewLayerCheckBox()36 ViewLayerCheckBox::~ViewLayerCheckBox()
37 {
38 }
39 
setViewLayer(ViewLayer * viewLayer)40 void ViewLayerCheckBox::setViewLayer(ViewLayer * viewLayer)
41 {
42 	m_viewLayer = viewLayer;
43 }
44 
viewLayer()45 ViewLayer * ViewLayerCheckBox::viewLayer() {
46 	return m_viewLayer;
47 }
48 
49 //////////////////////////////////////
50 
LayerPalette(QWidget * parent)51 LayerPalette::LayerPalette(QWidget * parent) : QScrollArea(parent)
52 {
53 
54 	m_hideAllLayersAct = m_showAllLayersAct = NULL;
55 
56 	QFrame * frame = new QFrame(this);
57 
58 	m_mainLayout = new QVBoxLayout();
59 	m_mainLayout->setSizeConstraint( QLayout::SetMinAndMaxSize );
60     m_mainLayout -> setObjectName("LayerWindowFrame");
61 	for (int i = 0; i < ViewLayer::ViewLayerCount; i++) {
62 		ViewLayerCheckBox * cb = new ViewLayerCheckBox(this);
63 		connect(cb, SIGNAL(clicked(bool)), this, SLOT(setLayerVisibility(bool)));
64 		m_checkBoxes.append(cb);
65             cb -> setObjectName("LayerCheckBox");
66 		cb->setVisible(false);
67 	}
68 
69 	m_groupBox = new QGroupBox("");
70     m_groupBox -> setObjectName("LayerWindowList");
71 	QVBoxLayout * groupLayout = new QVBoxLayout();
72     groupLayout -> setObjectName("LayerWindowBoxes");
73     m_showAllWidget = new QCheckBox(tr("show all layers"));
74 	connect(m_showAllWidget, SIGNAL(clicked(bool)), this, SLOT(setAllLayersVisible(bool)));
75 
76 	groupLayout->addWidget(m_showAllWidget);
77 
78 	m_groupBox->setLayout(groupLayout);
79 
80 	m_mainLayout->addWidget(m_groupBox);
81 
82 	frame->setLayout(m_mainLayout);
83 
84 	this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
85 	this->setWidget(frame);
86 
87 }
88 
~LayerPalette()89 LayerPalette::~LayerPalette()
90 {
91 }
92 
resetLayout(LayerHash & viewLayers,LayerList & keys)93 void LayerPalette::resetLayout(LayerHash & viewLayers, LayerList & keys) {
94 	m_mainLayout->removeWidget(m_groupBox);
95 	foreach (ViewLayerCheckBox * cb, m_checkBoxes) {
96 		m_mainLayout->removeWidget(cb);
97 		cb->setVisible(false);
98 	}
99 
100 	int ix = 0;
101 	foreach (ViewLayer::ViewLayerID key, keys) {
102 		ViewLayer * viewLayer = viewLayers.value(key);
103 		//if (viewLayer->parentLayer()) continue;
104 
105 		ViewLayerCheckBox * cb = m_checkBoxes[ix++];
106 		cb->setText(viewLayer->action()->text());
107 		cb->setViewLayer(viewLayer);
108 		cb->setVisible(true);
109 		m_mainLayout->addWidget(cb);
110 	}
111 
112 	m_mainLayout->addWidget(m_groupBox);
113 	m_mainLayout->invalidate();
114 
115 }
116 
updateLayerPalette(LayerHash & viewLayers,LayerList & keys)117 void LayerPalette::updateLayerPalette(LayerHash & viewLayers, LayerList & keys)
118 {
119 	m_showAllWidget->setEnabled(m_showAllLayersAct->isEnabled() || m_hideAllLayersAct->isEnabled());
120 	m_showAllWidget->setChecked(!m_showAllLayersAct->isEnabled());
121 
122 	int ix = 0;
123 	foreach (ViewLayer::ViewLayerID key, keys) {
124 		ViewLayer * viewLayer = viewLayers.value(key);
125 		//if (viewLayer->parentLayer()) continue;
126 
127 		ViewLayerCheckBox * cb = m_checkBoxes[ix++];
128 		cb->setChecked(viewLayer->action()->isChecked());
129 		cb->setEnabled(viewLayer->action()->isEnabled());
130 	}
131 }
132 
setLayerVisibility(bool)133 void LayerPalette::setLayerVisibility(bool) {
134 	ViewLayerCheckBox * cb = qobject_cast<ViewLayerCheckBox *>(sender());
135 	if (cb == NULL) return;
136 
137 	// want to toggle layer individually in this case
138 	cb->viewLayer()->setIncludeChildLayers(false);
139 	cb->viewLayer()->action()->trigger();
140 	cb->viewLayer()->setIncludeChildLayers(true);
141 }
142 
setShowAllLayersAction(QAction * action)143 void LayerPalette::setShowAllLayersAction(QAction * action)
144 {
145 	m_showAllLayersAct = action;
146 }
147 
setHideAllLayersAction(QAction * action)148 void LayerPalette::setHideAllLayersAction(QAction * action)
149 {
150 	m_hideAllLayersAct = action;
151 }
152 
setAllLayersVisible(bool vis)153 void LayerPalette::setAllLayersVisible(bool vis) {
154 	if (vis) {
155 		if (m_showAllLayersAct) {
156 			m_showAllLayersAct->trigger();
157 		}
158 	}
159 	else {
160 		if (m_hideAllLayersAct) {
161 			m_hideAllLayersAct->trigger();
162 		}
163 	}
164 }
165 
166