QGIS API Documentation
2.8.2-Wien
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
src
gui
symbology-ng
qgscolorrampcombobox.cpp
Go to the documentation of this file.
1
/***************************************************************************
2
qgscolorrampcombobox.cpp
3
---------------------
4
begin : October 2010
5
copyright : (C) 2010 by Martin Dobias
6
email : wonder dot sk at gmail dot com
7
***************************************************************************
8
* *
9
* This program is free software; you can redistribute it and/or modify *
10
* it under the terms of the GNU General Public License as published by *
11
* the Free Software Foundation; either version 2 of the License, or *
12
* (at your option) any later version. *
13
* *
14
***************************************************************************/
15
#include "
qgscolorrampcombobox.h
"
16
17
#include "
qgssymbollayerv2utils.h
"
18
#include "
qgsvectorcolorrampv2.h
"
19
#include "
qgsstylev2.h
"
20
#include "
qgsstylev2managerdialog.h
"
21
22
QSize
QgsColorRampComboBox::rampIconSize
( 50, 16 );
23
24
QgsColorRampComboBox::QgsColorRampComboBox
( QWidget *parent ) :
25
QComboBox( parent ), mStyle( NULL ), mSourceColorRamp( NULL ), mShowGradientOnly( false )
26
{
27
}
28
29
QgsColorRampComboBox::~QgsColorRampComboBox
()
30
{
31
delete
mSourceColorRamp
;
32
}
33
34
void
QgsColorRampComboBox::populate
(
QgsStyleV2
* style )
35
{
36
if
( count() != 0 )
37
return
;
// already populated!
38
39
mStyle
= style;
40
41
setIconSize(
rampIconSize
);
42
43
QStringList rampNames =
mStyle
->
colorRampNames
();
44
for
( QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it )
45
{
46
QgsVectorColorRampV2
* ramp = style->
colorRamp
( *it );
47
48
if
( !mShowGradientOnly || ramp->
type
() ==
"gradient"
)
49
{
50
QIcon icon =
QgsSymbolLayerV2Utils::colorRampPreviewIcon
( ramp,
rampIconSize
);
51
52
addItem( icon, *it );
53
}
54
delete
ramp;
55
}
56
57
if
( !mShowGradientOnly )
58
addItem(
tr
(
"Random colors"
) );
59
addItem(
tr
(
"New color ramp..."
) );
60
connect(
this
, SIGNAL( activated(
int
) ), SLOT(
colorRampChanged
(
int
) ) );
61
}
62
63
QgsVectorColorRampV2
*
QgsColorRampComboBox::currentColorRamp
()
64
{
65
QString rampName = currentText();
66
67
if
( rampName ==
tr
(
"Random colors"
) )
68
{
69
return
new
QgsRandomColorsV2
();
70
}
71
else
if
( rampName ==
"[source]"
&&
mSourceColorRamp
)
72
return
mSourceColorRamp
->
clone
();
73
else
74
return
mStyle
->
colorRamp
( rampName );
75
}
76
77
bool
QgsColorRampComboBox::createNewColorRampSelected
()
const
78
{
79
int
index
= currentIndex();
80
return
index == count() - 1;
//create new ramp is last item in combobox
81
}
82
83
void
QgsColorRampComboBox::setSourceColorRamp
(
QgsVectorColorRampV2
* sourceRamp )
84
{
85
mSourceColorRamp
= sourceRamp->
clone
();
86
87
QIcon icon =
QgsSymbolLayerV2Utils::colorRampPreviewIcon
(
mSourceColorRamp
,
rampIconSize
);
88
if
( itemText( 0 ) ==
"[source]"
)
89
setItemIcon( 0, icon );
90
else
91
insertItem( 0, icon,
"[source]"
);
92
setCurrentIndex( 0 );
93
}
94
95
void
QgsColorRampComboBox::colorRampChanged
(
int
index
)
96
{
97
if
( index != count() - 1 )
98
return
;
99
100
// last item: "new color ramp..."
101
QString rampName;
102
if
( !mShowGradientOnly )
103
{
104
rampName =
QgsStyleV2ManagerDialog::addColorRampStatic
(
this
,
mStyle
);
105
}
106
else
107
{
108
rampName =
QgsStyleV2ManagerDialog::addColorRampStatic
(
this
,
mStyle
,
"Gradient"
);
109
}
110
if
( rampName.isEmpty() )
111
return
;
112
113
// put newly added ramp into the combo
114
QgsVectorColorRampV2
* ramp =
mStyle
->
colorRamp
( rampName );
115
QIcon icon =
QgsSymbolLayerV2Utils::colorRampPreviewIcon
( ramp,
rampIconSize
);
116
117
blockSignals(
true
);
// avoid calling this method again!
118
insertItem( index, icon, rampName );
119
blockSignals(
false
);
120
121
delete
ramp;
122
123
// ... and set it as active
124
setCurrentIndex( index );
125
126
// make sure the color ramp is stored
127
mStyle
->
save
();
128
}
Generated on Sun May 10 2015 16:33:26 for QGIS API Documentation by
1.8.1.2