1 /*
2 Copyright (C) 2011 Elvis Stansvik <elvstone@gmail.com>
3 
4 For general Scribus (>=1.3.2) copyright and licensing information please refer
5 to the COPYING file provided with the program. Following this notice may exist
6 a copyright and/or license notice that predates the release of Scribus 1.3.2
7 for which a new license (GPL+exception) is in place.
8 */
9 
10 #include "iconmanager.h"
11 #include "scribus.h"
12 #include "scribusapp.h"
13 #include "smcellstylewidget.h"
14 
SMCellStyleWidget(QWidget * parent)15 SMCellStyleWidget::SMCellStyleWidget(QWidget *parent)
16 {
17 	setupUi(this);
18 
19 	fillColor->setPixmapType(ColorCombo::fancyPixmaps);
20 	fillColor->addItem(CommonStrings::tr_NoneColor);
21 
22 	iconSetChange();
23 
24 	connect(ScQApp, SIGNAL(iconSetChanged()), this, SLOT(iconSetChange()));
25 }
26 
~SMCellStyleWidget()27 SMCellStyleWidget::~SMCellStyleWidget()
28 {
29 }
30 
changeEvent(QEvent * e)31 void SMCellStyleWidget::changeEvent(QEvent *e)
32 {
33 	if (e->type() == QEvent::LanguageChange)
34 		languageChange();
35 	else
36 		QWidget::changeEvent(e);
37 }
38 
iconSetChange()39 void SMCellStyleWidget::iconSetChange()
40 {
41 	IconManager& iconManager = IconManager::instance();
42 	fillColorIcon->setPixmap(iconManager.loadPixmap("16/color-fill.png"));
43 	fillShadeLabel->setPixmap(iconManager.loadPixmap("shade.png") );
44 }
45 
languageChange()46 void SMCellStyleWidget::languageChange()
47 {
48 	retranslateUi(this);
49 
50 	if (fillColor->count() > 0)
51 	{
52 		bool fillColorBlocked = fillColor->blockSignals(true);
53 		fillColor->setItemText(0, CommonStrings::tr_NoneColor);
54 		fillColor->blockSignals(fillColorBlocked);
55 	}
56 }
57 
handleUpdateRequest(int updateFlags)58 void SMCellStyleWidget::handleUpdateRequest(int updateFlags)
59 {
60 	if (!m_Doc)
61 		return;
62 	if (updateFlags & reqColorsUpdate)
63 		fillFillColorCombo(m_Doc->PageColors);
64 }
65 
setDoc(ScribusDoc * doc)66 void SMCellStyleWidget::setDoc(ScribusDoc* doc)
67 {
68 	if (m_Doc)
69 		disconnect(m_Doc->scMW(), SIGNAL(UpdateRequest(int)), this , SLOT(handleUpdateRequest(int)));
70 
71 	m_Doc = doc;
72 	if (!m_Doc)
73 		return;
74 
75 	fillFillColorCombo(m_Doc->PageColors);
76 	connect(m_Doc->scMW(), SIGNAL(UpdateRequest(int)), this , SLOT(handleUpdateRequest(int)));
77 }
78 
show(CellStyle * cellStyle,QList<CellStyle> & cellStyles,const QString & defaultLanguage,int unitIndex)79 void SMCellStyleWidget::show(CellStyle *cellStyle, QList<CellStyle> &cellStyles, const QString &defaultLanguage, int unitIndex)
80 {
81 	Q_ASSERT(cellStyle);
82 	if (!cellStyle)
83 		return;
84 	parentCombo->setEnabled(!cellStyle->isDefaultStyle());
85 	const CellStyle *parent = dynamic_cast<const CellStyle*>(cellStyle->parentStyle());
86 	bool hasParent =  cellStyle->hasParent() && parent != nullptr && parent->hasName() && cellStyle->parent() != "";
87 	if (hasParent)
88 	{
89 		fillColor->setCurrentText(cellStyle->fillColor(), cellStyle->isInhFillColor());
90 		fillColor->setParentText(parent->fillColor());
91 		fillShade->setValue(qRound(cellStyle->fillShade()), cellStyle->isInhFillShade());
92 		fillShade->setParentValue(qRound(parent->fillShade()));
93 	}
94 	else
95 	{
96 		fillColor->setCurrentText(cellStyle->fillColor());
97 		fillShade->setValue(qRound(cellStyle->fillShade()));
98 	}
99 	parentCombo->clear();
100 	parentCombo->addItem( cellStyle->isDefaultStyle()? tr("A default style cannot be assigned a parent style") : "");
101 	if (!cellStyle->isDefaultStyle())
102 	{
103 		for (int i = 0; i < cellStyles.count(); ++i)
104 		{
105 			if (cellStyles[i].name() != cellStyle->name())
106 				parentCombo->addItem(cellStyles[i].name());
107 		}
108 	}
109 
110 	if (cellStyle->isDefaultStyle() || !hasParent)
111 		parentCombo->setCurrentIndex(0);
112 	else if (hasParent)
113 	{
114 		int index = 0;
115 		for (int i = 0; i < parentCombo->count(); ++i)
116 		{
117 			if (parentCombo->itemText(i) == cellStyle->parentStyle()->name())
118 			{
119 				index = i;
120 				break;
121 			}
122 		}
123 		parentCombo->setCurrentIndex(index);
124 	}
125 }
126 
show(QList<CellStyle * > & cellStyles,QList<CellStyle> & cellStylesAll,const QString & defaultLanguage,int unitIndex)127 void SMCellStyleWidget::show(QList<CellStyle*> &cellStyles, QList<CellStyle> &cellStylesAll, const QString &defaultLanguage, int unitIndex)
128 {
129 	if (cellStyles.count() == 1)
130 		show(cellStyles[0], cellStylesAll, defaultLanguage, unitIndex);
131 	else if (cellStyles.count() > 1)
132 	{
133 		showColors(cellStyles);
134 		parentCombo->setEnabled(false);
135 	}
136 }
137 
showColors(const QList<CellStyle * > & cellStyles)138 void SMCellStyleWidget::showColors(const QList<CellStyle*> &cellStyles)
139 {
140 	double d = -30000;
141 	for (int i = 0; i < cellStyles.count(); ++i)
142 	{
143 		if (d != -30000 && cellStyles[i]->fillShade() != d)
144 		{
145 			d = -30000;
146 			break;
147 		}
148 		d = cellStyles[i]->fillShade();
149 	}
150 	if (d == -30000)
151 		fillShade->setText( tr("Shade"));
152 	else
153 		fillShade->setValue(qRound(d));
154 	QString s;
155 	QString emptyString;
156 	for (int i = 0; i < cellStyles.count(); ++i)
157 	{
158 		if (!s.isNull() && s != cellStyles[i]->fillColor())
159 		{
160 			s = emptyString;
161 			break;
162 		}
163 		s = cellStyles[i]->fillColor();
164 	}
165 	if (s.isEmpty())
166 	{
167 		if (fillColor->itemText(fillColor->count() - 1) != "")
168 			fillColor->addItem("");
169 		fillColor->setCurrentIndex(fillColor->count() - 1);
170 	}
171 	else
172 		fillColor->setCurrentText(s);
173 }
174 
fillFillColorCombo(ColorList & colors)175 void SMCellStyleWidget::fillFillColorCombo(ColorList &colors)
176 {
177 	fillColor->clear();
178 
179 	fillColor->setColors(colors, true);
180 	fillColor->view()->setMinimumWidth(fillColor->view()->maximumViewportSize().width()+24);
181 }
182