21 return d_ptr->m_position;
26 return d_ptr->m_color;
31 return d_ptr->m_model;
36 d_ptr->m_color = color;
41 d_ptr->m_position = position;
45 : d_ptr(
new QtGradientStopPrivate())
47 d_ptr->m_model = model;
57 Q_DECLARE_PUBLIC(QtGradientStopsModel)
80 return d_ptr->m_posToStop;
85 if (d_ptr->m_posToStop.contains(pos))
86 return d_ptr->m_posToStop[pos];
92 PositionStopMap gradStops = stops();
93 if (gradStops.isEmpty())
94 return QColor::fromRgbF(pos, pos, pos, 1.0);
95 if (gradStops.contains(pos))
96 return gradStops[pos]->color();
99 auto itStop = gradStops.constFind(pos);
100 if (itStop == gradStops.constBegin()) {
102 return itStop.value()->color();
104 if (itStop == --gradStops.constEnd()) {
106 return itStop.value()->color();
108 auto itPrev = itStop;
109 auto itNext = itStop;
113 double prevX = itPrev.key();
114 double nextX = itNext.key();
116 double coefX = (pos - prevX) / (nextX - prevX);
117 QColor prevCol = itPrev.value()->color();
118 QColor nextCol = itNext.value()->color();
121 newColor.setRgbF((nextCol.redF() - prevCol.redF() ) * coefX + prevCol.redF(),
122 (nextCol.greenF() - prevCol.greenF()) * coefX + prevCol.greenF(),
123 (nextCol.blueF() - prevCol.blueF() ) * coefX + prevCol.blueF(),
124 (nextCol.alphaF() - prevCol.alphaF()) * coefX + prevCol.alphaF());
130 return d_ptr->m_selection.keys();
135 return d_ptr->m_current;
140 if (d_ptr->m_selection.contains(stop))
152 if (d_ptr->m_posToStop.contains(newPos))
155 stop->setPosition(newPos);
156 stop->setColor(color);
158 d_ptr->m_posToStop[newPos] = stop;
159 d_ptr->m_stopToPos[stop] = newPos;
161 emit stopAdded(stop);
168 if (!d_ptr->m_stopToPos.contains(stop))
174 emit stopRemoved(stop);
176 qreal pos = d_ptr->m_stopToPos[stop];
177 d_ptr->m_stopToPos.remove(stop);
178 d_ptr->m_posToStop.remove(pos);
184 if (!d_ptr->m_stopToPos.contains(stop))
186 if (d_ptr->m_posToStop.contains(newPos))
191 else if (newPos < 0.0)
194 emit stopMoved(stop, newPos);
196 const qreal oldPos = stop->position();
197 stop->setPosition(newPos);
198 d_ptr->m_stopToPos[stop] = newPos;
199 d_ptr->m_posToStop.remove(oldPos);
200 d_ptr->m_posToStop[newPos] = stop;
207 if (!d_ptr->m_stopToPos.contains(stop1))
209 if (!d_ptr->m_stopToPos.contains(stop2))
212 emit stopsSwapped(stop1, stop2);
214 const qreal pos1 = stop1->position();
215 const qreal pos2 = stop2->position();
216 stop1->setPosition(pos2);
217 stop2->setPosition(pos1);
218 d_ptr->m_stopToPos[stop1] = pos2;
219 d_ptr->m_stopToPos[stop2] = pos1;
220 d_ptr->m_posToStop[pos1] = stop2;
221 d_ptr->m_posToStop[pos2] = stop1;
226 if (!d_ptr->m_stopToPos.contains(stop))
228 if (stop->color() == newColor)
231 emit stopChanged(stop, newColor);
233 stop->setColor(newColor);
238 if (!d_ptr->m_stopToPos.contains(stop))
240 bool selected = d_ptr->m_selection.contains(stop);
241 if (select == selected)
244 emit stopSelected(stop, select);
247 d_ptr->m_selection[stop] =
true;
249 d_ptr->m_selection.remove(stop);
254 if (stop && !d_ptr->m_stopToPos.contains(stop))
259 emit currentStopChanged(stop);
261 d_ptr->m_current = stop;
266 PositionStopMap stopList = stops();
267 auto itStop = stopList.cbegin();
268 while (itStop != stopList.constEnd()) {
279 PositionStopMap stopList = stops();
280 auto itStop = stopList.cend();
281 while (itStop != stopList.constBegin()) {
295 QMap<qreal, QtGradientStop *> stopsToClone = stops();
296 for (
auto it = stopsToClone.cbegin(), end = stopsToClone.cend(); it != end; ++it)
297 model->addStop(it.key(), it.value()->color());
308 double newPos = newPosition;
315 if (newPos == current->position())
318 double offset = newPos - current->position();
324 double maxOffset = 1.0 - last->position();
325 double minOffset = -first->position();
327 if (offset > maxOffset)
329 else if (offset < minOffset)
337 bool forward = (offset > 0) ?
false :
true;
339 PositionStopMap stopList;
341 const auto selected = selectedStops();
342 for (QtGradientStop *stop : selected)
343 stopList[stop->position()] = stop;
344 stopList[current->position()] = current;
346 auto itStop = forward ? stopList.cbegin() : stopList.cend();
347 while (itStop != (forward ? stopList.constEnd() : stopList.constBegin())) {
351 double pos = stop->position() + offset;
361 if (oldStop && !stopList.values().contains(oldStop))
372 const auto stopsList = stops().values();
373 for (QtGradientStop *stop : stopsList)
379 const auto stopsList = selectedStops();
380 for (QtGradientStop *stop : stopsList)
381 selectStop(stop,
false);
386 QMap<qreal, QtGradientStop *> stopsMap = stops();
387 QHash<QtGradientStop *,
bool> swappedList;
388 for (
auto itStop = stopsMap.keyValueEnd(), begin = stopsMap.keyValueBegin(); itStop != begin;) {
391 if (swappedList.contains(stop))
393 const double newPos = 1.0 - (*itStop).first;
394 if (stopsMap.contains(newPos)) {
396 swappedList[swapped] =
true;
399 moveStop(stop, newPos);
406 const auto stopsMap = stops();
407 for (
auto it = stopsMap.cbegin(), end = stopsMap.cend(); it != end; ++it)
413 const auto selected = selectedStops();
414 for (QtGradientStop *stop : selected)
QtGradientStopsModel * m_model
QtGradientStopsModel * gradientModel() const
QHash< QtGradientStop *, bool > m_selection
QtGradientStop * m_current
QHash< QtGradientStop *, qreal > m_stopToPos
void selectStop(QtGradientStop *stop, bool select)
QList< QtGradientStop * > selectedStops() const
QtGradientStopsModel * clone() const
void removeStop(QtGradientStop *stop)
void changeStop(QtGradientStop *stop, QColor newColor)
QtGradientStop * at(qreal pos) const
bool isSelected(QtGradientStop *stop) const
QtGradientStop * currentStop() const
QtGradientStop * addStop(qreal pos, QColor color)
QtGradientStop * firstSelected() const
void moveStops(double newPosition)
QColor color(qreal pos) const
QtGradientStopsModel(QObject *parent=0)
void swapStops(QtGradientStop *stop1, QtGradientStop *stop2)
PositionStopMap stops() const
QtGradientStop * lastSelected() const
void moveStop(QtGradientStop *stop, qreal newPos)
void setCurrentStop(QtGradientStop *stop)
Combined button and popup list for selecting options.