KisPaintOpPresetsChooserPopup::KisPaintOpPresetsChooserPopup(QWidget * parent)
    : QWidget(parent)
    , m_d(new Private())
{
    m_d->uiWdgPaintOpPresets.setupUi(this);
    KMenu* menu = new KMenu(this);

    QActionGroup *actionGroup = new QActionGroup(this);

    KisPresetChooser::ViewMode mode = (KisPresetChooser::ViewMode)KisConfig().presetChooserViewMode();
    bool showAll = KisConfig().presetShowAllMode();

    QAction* action = menu->addAction(koIcon("view-preview"), i18n("Thumbnails"), this, SLOT(slotThumbnailMode()));
    action->setCheckable(true);
    action->setChecked(mode == KisPresetChooser::THUMBNAIL);
    action->setActionGroup(actionGroup);

    action = menu->addAction(koIcon("view-list-details"), i18n("Details"), this, SLOT(slotDetailMode()));
    action->setCheckable(true);
    action->setChecked(mode == KisPresetChooser::DETAIL);
    action->setActionGroup(actionGroup);

    m_d->uiWdgPaintOpPresets.viewModeButton->setIcon(koIcon("view-choose"));
    m_d->uiWdgPaintOpPresets.viewModeButton->setMenu(menu);
    m_d->uiWdgPaintOpPresets.viewModeButton->setPopupMode(QToolButton::InstantPopup);
    m_d->uiWdgPaintOpPresets.wdgPresetChooser->setViewMode(mode);
    m_d->uiWdgPaintOpPresets.wdgPresetChooser->showTaggingBar(false,true);

    connect(m_d->uiWdgPaintOpPresets.wdgPresetChooser, SIGNAL(resourceSelected(KoResource*)),
            this, SIGNAL(resourceSelected(KoResource*)));

    connect(m_d->uiWdgPaintOpPresets.searchBar, SIGNAL(textChanged(QString)),
            m_d->uiWdgPaintOpPresets.wdgPresetChooser, SLOT(searchTextChanged(QString)));

    connect(m_d->uiWdgPaintOpPresets.searchBar, SIGNAL(textChanged(QString)),
                this, SLOT(setLineEditCompleter(QString)));

    connect(m_d->uiWdgPaintOpPresets.searchBar, SIGNAL(returnPressed(QString)),
                this, SLOT(returnKeyPressed(QString)));

    connect(m_d->uiWdgPaintOpPresets.showAllCheckBox, SIGNAL(toggled(bool)),
            m_d->uiWdgPaintOpPresets.wdgPresetChooser, SLOT(setShowAll(bool)));
    m_d->firstShown = true;

    m_d->uiWdgPaintOpPresets.showAllCheckBox->setChecked(showAll);
}
KisPaletteManager::KisPaletteManager(KoFavoriteResourceManager *manager, KisPaintopBox *paintOpBox)
    : QDialog(paintOpBox)
    , m_saveButton(0)
    , m_removeButton(0)
    , m_resourceManager(manager)
    , m_paintOpBox(paintOpBox)
{
    setWindowTitle(i18n("Palette Manager"));

    m_allPresetsView = new KisPresetChooser(this);
    m_allPresetsView->showButtons(false);
    m_allPresetsView->showTaggingBar(false,false);

    m_palettePresetsView = new KisPresetChooser(this);
    m_palettePresetsView->showButtons(false);
    m_palettePresetsView->showTaggingBar(false,false);

    /*LEFT COMPONENTS*/
    QFrame *HSeparator = new QFrame();
    HSeparator->setFrameStyle(QFrame::HLine | QFrame::Sunken);

    m_saveButton = new QPushButton (i18n("Add to Palette"));
    m_saveButton->setSizePolicy(QSizePolicy::Fixed , QSizePolicy::Fixed);
    m_saveButton->setEnabled(false);

    /*LEFT LAYOUT*/
    QVBoxLayout *leftLayout = new QVBoxLayout ();
    leftLayout->addWidget(new QLabel(i18n("Available Presets")));
    leftLayout->addWidget(m_allPresetsView);
    leftLayout->addWidget(m_saveButton);

    m_allPresetsView->updateViewSettings();

    /*CENTER COMPONENT : Divider*/
    QFrame *VSeparator = new QFrame();
    VSeparator->setFrameStyle(QFrame::VLine | QFrame::Sunken);

    /*RIGHT COMPONENTS*/
    m_removeButton = new QPushButton(i18n("Remove Preset"));
    m_removeButton->setSizePolicy(QSizePolicy::Fixed , QSizePolicy::Fixed);
    m_removeButton->setEnabled(false);//set the button to center

    /*RIGHT LAYOUT*/
    QVBoxLayout *rightLayout = new QVBoxLayout();
    rightLayout->addWidget(new QLabel(i18n("Favorite Presets")));
    rightLayout->addWidget(m_palettePresetsView);

    /*RIGHT BOTTOM LAYOUT*/
    QHBoxLayout *rightBottomLayout = new QHBoxLayout();
    rightBottomLayout->addWidget(m_removeButton);
    rightBottomLayout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding));
    QToolButton* viewButton = new QToolButton();
    viewButton->setPopupMode(QToolButton::InstantPopup);
    rightBottomLayout->addWidget(viewButton);
    rightLayout->addLayout(rightBottomLayout);

    QMenu* menu = new QMenu(this);

    QActionGroup *actionGroup = new QActionGroup(this);

    KConfigGroup group(KGlobal::config(), "favoriteList");
    KisPresetChooser::ViewMode mode = (KisPresetChooser::ViewMode)group.readEntry("presetChooserViewMode", 0);
    m_allPresetsView->setViewMode(mode);
    m_palettePresetsView->setViewMode(mode);

    QAction* action = menu->addAction(koIcon("view-preview"), i18n("Thumbnails"), this, SLOT(slotThumbnailMode()));
    action->setCheckable(true);
    action->setChecked(mode == KisPresetChooser::THUMBNAIL);
    action->setActionGroup(actionGroup);

    action = menu->addAction(koIcon("view-list-details"), i18n("Details"), this, SLOT(slotDetailMode()));
    action->setCheckable(true);
    action->setChecked(mode == KisPresetChooser::DETAIL);
    action->setActionGroup(actionGroup);

    viewButton->setIcon(koIcon("view-choose"));
    viewButton->setMenu(menu);

    /*MAIN LAYOUT*/
    QHBoxLayout *mainLayout = new QHBoxLayout();
    mainLayout->addLayout(leftLayout);
    mainLayout->addWidget(VSeparator);
    mainLayout->addLayout(rightLayout);

    setLayout(mainLayout);

    /*SIGNALS AND SLOTS*/
    connect(m_allPresetsView, SIGNAL(resourceSelected(KoResource*)), this, SLOT(slotUpdateAddButton()) );
    connect(m_palettePresetsView, SIGNAL(resourceSelected(KoResource*)), this, SLOT(slotEnableRemoveButton()) );
    connect(m_removeButton, SIGNAL(clicked()), this, SLOT(slotDeleteBrush()));
    connect(m_saveButton, SIGNAL(clicked()), this, SLOT(slotAddBrush()));

    updatePaletteView();
}