dlg_bundle_manager.cpp 16.4 KB
Newer Older
Boudewijn Rempt's avatar
Boudewijn Rempt committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 *  Copyright (c) 2014 Victor Lafon metabolic.ewilan@hotmail.fr
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#include "dlg_bundle_manager.h"
#include "ui_wdgdlgbundlemanager.h"

#include "resourcemanager.h"
23
#include "dlg_create_bundle.h"
Boudewijn Rempt's avatar
Boudewijn Rempt committed
24 25

#include <QListWidget>
26
#include <QTreeWidget>
Boudewijn Rempt's avatar
Boudewijn Rempt committed
27 28
#include <QListWidgetItem>
#include <QPainter>
29
#include <QPixmap>
30
#include <QMessageBox>
Boudewijn Rempt's avatar
Boudewijn Rempt committed
31

32
#include <kis_icon.h>
33
#include "kis_action.h"
34
#include <KisResourceServerProvider.h>
35 36
#include <kconfiggroup.h>
#include <ksharedconfig.h>
Boudewijn Rempt's avatar
Boudewijn Rempt committed
37 38 39

#define ICON_SIZE 48

40
DlgBundleManager::DlgBundleManager(ResourceManager *resourceManager, KisActionManager* actionMgr, QWidget *parent)
41
    : KoDialog(parent)
Boudewijn Rempt's avatar
Boudewijn Rempt committed
42 43
    , m_page(new QWidget())
    , m_ui(new Ui::WdgDlgBundleManager)
44
    , m_currentBundle(0)
45
    , m_resourceManager(resourceManager)
Boudewijn Rempt's avatar
Boudewijn Rempt committed
46 47 48 49 50 51 52 53
{
    setCaption(i18n("Manage Resource Bundles"));
    m_ui->setupUi(m_page);
    setMainWidget(m_page);
    resize(m_page->sizeHint());
    setButtons(Ok | Cancel);
    setDefaultButton(Ok);

54
    m_ui->listActive->setIconSize(QSize(ICON_SIZE, ICON_SIZE));
55
    m_ui->listActive->setSelectionMode(QAbstractItemView::SingleSelection);
56
    connect(m_ui->listActive, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), SLOT(itemSelected(QListWidgetItem*,QListWidgetItem*)));
57
    connect(m_ui->listActive, SIGNAL(itemClicked(QListWidgetItem*)), SLOT(itemSelected(QListWidgetItem*)));
58 59

    m_ui->listInactive->setIconSize(QSize(ICON_SIZE, ICON_SIZE));
60
    m_ui->listInactive->setSelectionMode(QAbstractItemView::SingleSelection);
61
    connect(m_ui->listInactive, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), SLOT(itemSelected(QListWidgetItem*,QListWidgetItem*)));
62
    connect(m_ui->listInactive, SIGNAL(itemClicked(QListWidgetItem*)), SLOT(itemSelected(QListWidgetItem*)));
63

64
    m_ui->bnAdd->setIcon(KisIconUtils::loadIcon("arrow-right"));
Boudewijn Rempt's avatar
Boudewijn Rempt committed
65 66
    connect(m_ui->bnAdd, SIGNAL(clicked()), SLOT(addSelected()));

67
    m_ui->bnRemove->setIcon(KisIconUtils::loadIcon("arrow-left"));
Boudewijn Rempt's avatar
Boudewijn Rempt committed
68 69
    connect(m_ui->bnRemove, SIGNAL(clicked()), SLOT(removeSelected()));

70
    m_ui->listBundleContents->setHeaderLabel(i18n("Resource"));
71
    m_ui->listBundleContents->setSelectionMode(QAbstractItemView::NoSelection);
Boudewijn Rempt's avatar
Boudewijn Rempt committed
72

73
    m_actionManager = actionMgr;
Boudewijn Rempt's avatar
Boudewijn Rempt committed
74

75 76 77 78
    refreshListData();

    connect(m_ui->bnEditBundle, SIGNAL(clicked()), SLOT(editBundle()));

79 80 81 82 83 84 85 86 87
    connect(m_ui->bnImportBrushes, SIGNAL(clicked()), SLOT(slotImportResource()));
    connect(m_ui->bnImportGradients, SIGNAL(clicked()), SLOT(slotImportResource()));
    connect(m_ui->bnImportPalettes, SIGNAL(clicked()), SLOT(slotImportResource()));
    connect(m_ui->bnImportPatterns, SIGNAL(clicked()), SLOT(slotImportResource()));
    connect(m_ui->bnImportPresets, SIGNAL(clicked()), SLOT(slotImportResource()));
    connect(m_ui->bnImportWorkspaces, SIGNAL(clicked()), SLOT(slotImportResource()));
    connect(m_ui->bnImportBundles, SIGNAL(clicked()), SLOT(slotImportResource()));


88 89 90 91 92 93 94 95 96
    connect(m_ui->createBundleButton, SIGNAL(clicked()), SLOT(slotCreateBundle()));
    connect(m_ui->deleteBackupFilesButton, SIGNAL(clicked()), SLOT(slotDeleteBackupFiles()));
    connect(m_ui->openResourceFolderButton, SIGNAL(clicked()), SLOT(slotOpenResourceFolder()));

}


void DlgBundleManager::refreshListData()
{
97
    KoResourceServer<KisResourceBundle> *bundleServer = KisResourceBundleServerProvider::instance()->resourceBundleServer();
98 99 100 101

    m_ui->listInactive->clear();
    m_ui->listActive->clear();

102
    Q_FOREACH (const QString &f, bundleServer->blackListedFiles()) {
103
        KisResourceBundle *bundle = new KisResourceBundle(f);
Boudewijn Rempt's avatar
Boudewijn Rempt committed
104 105
        bundle->load();
        if (bundle->valid()) {
106
            bundle->setInstalled(false);
Boudewijn Rempt's avatar
Boudewijn Rempt committed
107 108 109 110 111
            m_blacklistedBundles[f] = bundle;
        }
    }
    fillListWidget(m_blacklistedBundles.values(), m_ui->listInactive);

112
    Q_FOREACH (KisResourceBundle *bundle, bundleServer->resources()) {
Boudewijn Rempt's avatar
Boudewijn Rempt committed
113 114 115 116 117 118 119
        if (bundle->valid()) {
            m_activeBundles[bundle->filename()] = bundle;
        }
    }
    fillListWidget(m_activeBundles.values(), m_ui->listActive);
}

120 121
void DlgBundleManager::accept()
{
122
    KoResourceServer<KisResourceBundle> *bundleServer = KisResourceBundleServerProvider::instance()->resourceBundleServer();
123 124 125 126

    for (int i = 0; i < m_ui->listActive->count(); ++i) {
        QListWidgetItem *item = m_ui->listActive->item(i);
        QByteArray ba = item->data(Qt::UserRole).toByteArray();
127
        QString name = item->text();
128
        KisResourceBundle *bundle = bundleServer->resourceByMD5(ba);
129
        QMessageBox bundleFeedback;
130 131
        bundleFeedback.setIcon(QMessageBox::Warning);
        QString feedback = "bundlefeedback";
132

133 134
        if (!bundle) {
            // Get it from the blacklisted bundles
135
            Q_FOREACH (KisResourceBundle *b2, m_blacklistedBundles.values()) {
136 137 138 139 140 141
                if (b2->md5() == ba) {
                    bundle = b2;
                    break;
                }
            }
        }
142

143
        if (bundle) {
144
            bool isKrita3Bundle = false;
145
            if (bundle->filename().endsWith("Krita_3_Default_Resources.bundle")) {
146 147 148
                isKrita3Bundle = true;
                KConfigGroup group = KSharedConfig::openConfig()->group("BundleHack");
                group.writeEntry("HideKrita3Bundle", false);
149 150
            }
            else {
151 152 153 154 155
                if (!bundle->isInstalled()) {
                    bundle->install();
                    //this removes the bundle from the blacklist and add it to the server without saving or putting it in front//
                    if (!bundleServer->addResource(bundle, false, false)){

156
                        feedback = i18n("Couldn't add bundle \"%1\" to resource server", name);
157 158 159 160 161
                        bundleFeedback.setText(feedback);
                        bundleFeedback.exec();
                    }
                    if (!isKrita3Bundle) {
                        if (!bundleServer->removeFromBlacklist(bundle)) {
162
                            feedback = i18n("Couldn't remove bundle \"%1\" from blacklist", name);
163 164 165 166 167 168 169 170 171 172 173
                            bundleFeedback.setText(feedback);
                            bundleFeedback.exec();
                        }
                    }
                }
                else {
                    if (!isKrita3Bundle) {
                        bundleServer->removeFromBlacklist(bundle);
                    }
                    //let's assume that bundles that exist and are installed have to be removed from the blacklist, and if they were already this returns false, so that's not a problem.
                }
174 175 176
            }
        }
        else{
177
            QString feedback = i18n("Bundle \"%1\" doesn't exist!", name);
178 179
            bundleFeedback.setText(feedback);
            bundleFeedback.exec();
180

181 182 183 184 185 186
        }
    }

    for (int i = 0; i < m_ui->listInactive->count(); ++i) {
        QListWidgetItem *item = m_ui->listInactive->item(i);
        QByteArray ba = item->data(Qt::UserRole).toByteArray();
187
        KisResourceBundle *bundle = bundleServer->resourceByMD5(ba);
188 189 190 191 192 193 194 195 196 197 198 199 200
        bool isKrits3Bundle = false;
        if (bundle) {
            if (bundle->filename().contains("Krita_3_Default_Resources.bundle")) {
                isKrits3Bundle = true;
                KConfigGroup group = KSharedConfig::openConfig()->group("BundleHack");
                group.writeEntry("HideKrita3Bundle", true);
            }
            if (bundle->isInstalled()) {
                bundle->uninstall();
                if (!isKrits3Bundle) {
                    bundleServer->removeResourceAndBlacklist(bundle);
                }
            }
201 202 203 204
        }
    }


205
    KoDialog::accept();
206 207
}

Boudewijn Rempt's avatar
Boudewijn Rempt committed
208 209
void DlgBundleManager::addSelected()
{
210
    Q_FOREACH (QListWidgetItem *item, m_ui->listActive->selectedItems()) {
211 212 213
        m_ui->listInactive->addItem(m_ui->listActive->takeItem(m_ui->listActive->row(item)));
    }

Boudewijn Rempt's avatar
Boudewijn Rempt committed
214 215 216 217
}

void DlgBundleManager::removeSelected()
{
218
    Q_FOREACH (QListWidgetItem *item, m_ui->listInactive->selectedItems()) {
219 220
        m_ui->listActive->addItem(m_ui->listInactive->takeItem(m_ui->listInactive->row(item)));
    }
Boudewijn Rempt's avatar
Boudewijn Rempt committed
221 222
}

223
void DlgBundleManager::itemSelected(QListWidgetItem *current, QListWidgetItem *)
224
{
225
    if (!current) {
226 227 228 229 230 231 232 233
        m_ui->lblName->clear();
        m_ui->lblAuthor->clear();
        m_ui->lblEmail->clear();
        m_ui->lblLicense->clear();
        m_ui->lblWebsite->clear();
        m_ui->lblDescription->clear();
        m_ui->lblCreated->clear();
        m_ui->lblUpdated->clear();
234 235
        m_ui->lblPreview->setPixmap(QPixmap::fromImage(QImage()));
        m_ui->listBundleContents->clear();
236
        m_ui->bnEditBundle->setEnabled(false);
237
        m_currentBundle = 0;
238 239 240 241
    }
    else {

        QByteArray ba = current->data(Qt::UserRole).toByteArray();
242
        KoResourceServer<KisResourceBundle> *bundleServer = KisResourceBundleServerProvider::instance()->resourceBundleServer();
243
        KisResourceBundle *bundle = bundleServer->resourceByMD5(ba);
244

245 246
        if (!bundle) {
            // Get it from the blacklisted bundles
247
            Q_FOREACH (KisResourceBundle *b2, m_blacklistedBundles.values()) {
248 249 250 251 252 253 254
                if (b2->md5() == ba) {
                    bundle = b2;
                    break;
                }
            }
        }

255

256
        if (bundle) {
257
            QFontMetrics metrics(this->font());
258 259

            m_currentBundle = bundle;
260
            m_ui->bnEditBundle->setEnabled(true);
261

262
            m_ui->lblName->setText(bundle->name());
263 264 265 266 267 268 269 270
            m_ui->lblAuthor->setText(metrics.elidedText(bundle->getMeta("author"), Qt::ElideRight, m_ui->lblAuthor->width()));
            m_ui->lblAuthor->setToolTip(bundle->getMeta("author"));
            m_ui->lblEmail->setText(metrics.elidedText(bundle->getMeta("email"), Qt::ElideRight, m_ui->lblEmail->width()));
            m_ui->lblEmail->setToolTip(bundle->getMeta("email"));
            m_ui->lblLicense->setText(metrics.elidedText(bundle->getMeta("license"), Qt::ElideRight, m_ui->lblLicense->width()));
            m_ui->lblLicense->setToolTip(bundle->getMeta("license"));
            m_ui->lblWebsite->setText(metrics.elidedText(bundle->getMeta("website"), Qt::ElideRight, m_ui->lblWebsite->width()));
            m_ui->lblWebsite->setToolTip(bundle->getMeta("website"));
271
            m_ui->lblDescription->setPlainText(bundle->getMeta("description"));
272 273 274 275 276 277 278 279 280 281
            if (QDateTime::fromString(bundle->getMeta("created")).isValid()) {
                m_ui->lblCreated->setText(QDateTime::fromString(bundle->getMeta("created")).toString());
            } else {
                m_ui->lblCreated->setText(QDate::fromString(bundle->getMeta("created"), "dd/MM/yyyy").toString());
            }
            if (QDateTime::fromString(bundle->getMeta("updated")).isValid()) {
                m_ui->lblCreated->setText(QDateTime::fromString(bundle->getMeta("updated")).toString());
            } else {
                m_ui->lblCreated->setText(QDate::fromString(bundle->getMeta("updated"), "dd/MM/yyyy").toString());
            }
282
            m_ui->lblPreview->setPixmap(QPixmap::fromImage(bundle->image().scaled(128, 128, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
283 284
            m_ui->listBundleContents->clear();

285
            Q_FOREACH (const QString & resType, bundle->resourceTypes()) {
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

                QTreeWidgetItem *toplevel = new QTreeWidgetItem();
                if (resType == "gradients") {
                    toplevel->setText(0, i18n("Gradients"));
                }
                else if (resType  == "patterns") {
                    toplevel->setText(0, i18n("Patterns"));
                }
                else if (resType  == "brushes") {
                    toplevel->setText(0, i18n("Brushes"));
                }
                else if (resType  == "palettes") {
                    toplevel->setText(0, i18n("Palettes"));
                }
                else if (resType  == "workspaces") {
                    toplevel->setText(0, i18n("Workspaces"));
                }
                else if (resType  == "paintoppresets") {
                    toplevel->setText(0, i18n("Brush Presets"));
                }
306 307 308 309
                else if (resType  == "gamutmasks") {
                    toplevel->setText(0, i18n("Gamut Masks"));
                }

310

311
                m_ui->listBundleContents->addTopLevelItem(toplevel);
312

313
                Q_FOREACH (const KoResource *res, bundle->resources(resType)) {
314 315 316 317 318 319
                    if (res) {
                        QTreeWidgetItem *i = new QTreeWidgetItem();
                        i->setIcon(0, QIcon(QPixmap::fromImage(res->image())));
                        i->setText(0, res->name());
                        toplevel->addChild(i);
                    }
320
                }
321
            }
322
        }
323 324 325
        else {
            m_currentBundle = 0;
        }
326
    }
327 328
}

329 330 331 332 333
void DlgBundleManager::itemSelected(QListWidgetItem *current)
{
    itemSelected(current, 0);
}

334 335 336 337
void DlgBundleManager::editBundle()
{
    if (m_currentBundle) {
        DlgCreateBundle dlg(m_currentBundle);
338 339
        m_activeBundles.remove(m_currentBundle->filename());
        m_currentBundle = 0;
340 341 342
        if (dlg.exec() != QDialog::Accepted) {
            return;
        }
343 344
        m_currentBundle = m_resourceManager->saveBundle(dlg);
        refreshListData();
345 346 347
    }
}

348
void DlgBundleManager::fillListWidget(QList<KisResourceBundle *> bundles, QListWidget *w)
Boudewijn Rempt's avatar
Boudewijn Rempt committed
349 350 351 352
{
    w->setIconSize(QSize(ICON_SIZE, ICON_SIZE));
    w->setSelectionMode(QAbstractItemView::MultiSelection);

353
    Q_FOREACH (KisResourceBundle *bundle, bundles) {
Boudewijn Rempt's avatar
Boudewijn Rempt committed
354
        QPixmap pixmap(ICON_SIZE, ICON_SIZE);
355
        pixmap.fill(Qt::gray);
Boudewijn Rempt's avatar
Boudewijn Rempt committed
356 357 358 359 360 361 362 363 364 365
        if (!bundle->image().isNull()) {
            QImage scaled = bundle->image().scaled(ICON_SIZE, ICON_SIZE, Qt::KeepAspectRatio, Qt::SmoothTransformation);
            int x = (ICON_SIZE - scaled.width()) / 2;
            int y = (ICON_SIZE - scaled.height()) / 2;
            QPainter gc(&pixmap);
            gc.drawImage(x, y, scaled);
            gc.end();
        }

        QListWidgetItem *item = new QListWidgetItem(pixmap, bundle->name());
366
        item->setData(Qt::UserRole, bundle->md5());
Boudewijn Rempt's avatar
Boudewijn Rempt committed
367 368 369 370
        w->addItem(item);
    }
}

371

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
void DlgBundleManager::slotImportResource()
{
    if (m_actionManager) {
        QObject *button = sender();
        QString buttonName = button->objectName();
        KisAction *action = 0;
        if (buttonName == "bnImportBundles") {
            action = m_actionManager->actionByName("import_bundles");
        }
        else if (buttonName == "bnImportBrushes") {
            action = m_actionManager->actionByName("import_brushes");
        }
        else if (buttonName == "bnImportGradients") {
            action = m_actionManager->actionByName("import_gradients");
        }
        else if (buttonName == "bnImportPalettes") {
            action = m_actionManager->actionByName("import_palettes");
        }
        else if (buttonName == "bnImportPatterns") {
            action = m_actionManager->actionByName("import_patterns");
        }
        else if (buttonName == "bnImportPresets") {
            action = m_actionManager->actionByName("import_presets");
        }
        else if (buttonName == "bnImportWorkspaces") {
            action = m_actionManager->actionByName("import_workspaces");
        }
399 400 401 402
        else {
            warnUI << "Unhandled bundle manager import button " << buttonName;
            return;
        }
403 404

        action->trigger();
405
        refreshListData();
406 407 408 409 410
    }
}

void DlgBundleManager::slotCreateBundle() {

411
    if (m_actionManager) {
412 413
        KisAction *action = m_actionManager->actionByName("create_bundle");
        action->trigger();
414
        refreshListData();
415 416 417 418 419
    }
}

void DlgBundleManager::slotDeleteBackupFiles() {

420
    if (m_actionManager) {
421 422 423 424 425 426 427
        KisAction *action = m_actionManager->actionByName("edit_blacklist_cleanup");
        action->trigger();
    }
}

void DlgBundleManager::slotOpenResourceFolder() {

428
    if (m_actionManager) {
429 430 431 432 433
        KisAction *action = m_actionManager->actionByName("open_resources_directory");
        action->trigger();
    }
}