Several fixes in gradients UI

parent ef81d598
......@@ -31,7 +31,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "utils/KoIconUtils.h"
GradientWidget::GradientWidget(QMap <QString, QString> gradients, QWidget *parent) :
GradientWidget::GradientWidget(QMap <QString, QString> gradients, int ix, QWidget *parent) :
QDialog(parent),
Ui::GradientEdit_UI()
{
......@@ -51,6 +51,7 @@ GradientWidget::GradientWidget(QMap <QString, QString> gradients, QWidget *paren
gradient_list->setIconSize(QSize(6 * m_height, m_height));
connect(gradient_list, SIGNAL(currentRowChanged(int)), this, SLOT(loadGradient()));
loadGradients(gradients);
gradient_list->setCurrentRow(ix);
}
void GradientWidget::resizeEvent(QResizeEvent* event)
......@@ -121,9 +122,18 @@ void GradientWidget::updatePreview()
painter.fillPath(path, br2);
painter.end();
preview->setPixmap(p);
// save changes to currently active gradient
QListWidgetItem *current = gradient_list->currentItem();
if (!current) return;
saveGradient(current->text());
}
void GradientWidget::saveGradient()
int GradientWidget::selectedGradient() const
{
return gradient_list->currentRow();
}
void GradientWidget::saveGradient(const QString &name)
{
QPixmap pix(6 * m_height, m_height);
pix.fill(Qt::transparent);
......@@ -133,16 +143,23 @@ void GradientWidget::saveGradient()
painter.fillRect(0, 0, pix.width(), pix.height(), QBrush(m_gradient));
painter.end();
QIcon icon(pix);
int ct = gradient_list->count();
QStringList existing = getNames();
QString test = i18n("Gradient %1", ct);
while (existing.contains(test)) {
ct++;
test = i18n("Gradient %1", ct);
QListWidgetItem *item = NULL;
if (!name.isEmpty()) {
item = gradient_list->currentItem();
item->setIcon(icon);
} else {
// Create new gradient
int ct = gradient_list->count();
QStringList existing = getNames();
QString test = i18n("Gradient %1", ct);
while (existing.contains(test)) {
ct++;
test = i18n("Gradient %1", ct);
}
item = new QListWidgetItem(icon, test, gradient_list);
item->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled);
}
QListWidgetItem *item = new QListWidgetItem(icon, test, gradient_list);
item->setData(Qt::UserRole, gradientToString());
item->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled);
}
QStringList GradientWidget::getNames() const
......
......@@ -42,12 +42,13 @@ public:
* @param projectPath default path to save to or load from title documents
* @param render project renderer
* @param parent (optional) parent widget */
explicit GradientWidget(QMap <QString, QString> gradients = QMap<QString, QString>(), QWidget *parent = 0);
explicit GradientWidget(QMap <QString, QString> gradients = QMap<QString, QString>(), int ix = 0, QWidget *parent = 0);
void resizeEvent(QResizeEvent* event);
QString gradientToString() const;
static QLinearGradient gradientFromString(const QString &str, int width, int height);
QMap <QString, QString> gradients() const;
QList <QIcon> icons() const;
int selectedGradient() const;
private:
QLinearGradient m_gradient;
......@@ -57,7 +58,7 @@ private:
private slots:
void updatePreview();
void saveGradient();
void saveGradient(const QString &name = QString());
void loadGradient();
void deleteGradient();
};
......
......@@ -2744,11 +2744,19 @@ void TitleWidget::prepareTools(QGraphicsItem *referenceItem)
void TitleWidget::slotEditGradient()
{
QToolButton *caller = qobject_cast<QToolButton *>(QObject::sender());
if (!caller) return;
QComboBox *combo = NULL;
if (caller == edit_gradient) {
combo = gradients_combo;
} else {
combo = gradients_rect_combo;
}
QMap <QString, QString> gradients;
for (int i = 0; i < gradients_combo->count(); i++) {
gradients.insert(gradients_combo->itemText(i), gradients_combo->itemData(i).toString());
for (int i = 0; i < combo->count(); i++) {
gradients.insert(combo->itemText(i), combo->itemData(i).toString());
}
GradientWidget d(gradients);
GradientWidget d(gradients, combo->currentIndex());
if (d.exec() == QDialog::Accepted) {
// Save current gradients
QMap <QString, QString> gradients = d.gradients();
......@@ -2757,7 +2765,7 @@ void TitleWidget::slotEditGradient()
KSharedConfigPtr config = KSharedConfig::openConfig();
KConfigGroup group(config, "TitleGradients");
group.deleteGroup();
gradients_combo->clear();
combo->clear();
gradients_rect_combo->clear();
int ix = 0;
while (i != gradients.constEnd()) {
......@@ -2768,6 +2776,7 @@ void TitleWidget::slotEditGradient()
ix++;
}
group.sync();
combo->setCurrentIndex(d.selectedGradient());
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment