tagguesserconfigdlg.cpp 4.85 KB
Newer Older
1 2
/**
 * Copyright (C) 2003 Frerich Raabe <raabe@kde.org>
3
 *
4 5 6 7 8 9 10 11 12 13 14
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
15
 */
16

17
#include "tagguesserconfigdlg.h"
18
#include "tagguesser.h"
19

Pino Toscano's avatar
Pino Toscano committed
20
#include <kicon.h>
21 22
#include <klocale.h>
#include <kpushbutton.h>
23 24
#include <klineedit.h>
#include <kapplication.h>
25

Laurent Montel's avatar
Laurent Montel committed
26
#include <QKeyEvent>
27
#include <QStringListModel>
28 29

TagGuesserConfigDlg::TagGuesserConfigDlg(QWidget *parent, const char *name)
30
    : KDialog(parent)
31
{
32
    setObjectName( QLatin1String( name ) );
33 34 35 36
    setModal(true);
    setCaption(i18n("Tag Guesser Configuration"));
    setButtons(Ok | Cancel);
    setDefaultButton(Ok);
Laurent Montel's avatar
Laurent Montel committed
37
    showButtonSeparator(true);
38

Laurent Montel's avatar
Laurent Montel committed
39
    m_child = new TagGuesserConfigDlgWidget(this);
40 41
    setMainWidget(m_child);

42 43
    m_child->bMoveUp->setIcon(KIcon( QLatin1String( "arrow-up" )));
    m_child->bMoveDown->setIcon(KIcon( QLatin1String( "arrow-down" )));
44

45 46 47 48 49 50
    m_tagSchemeModel = new QStringListModel(m_child->lvSchemes);
    m_child->lvSchemes->setModel(m_tagSchemeModel);
    m_child->lvSchemes->setHeaderHidden(true);
    m_tagSchemeModel->setStringList(TagGuesser::schemeStrings());

    connect(m_child->lvSchemes, SIGNAL(clicked(QModelIndex)), this, SLOT(slotCurrentChanged(QModelIndex)));
51 52
    connect(m_child->bMoveUp, SIGNAL(clicked()), this, SLOT(slotMoveUpClicked()));
    connect(m_child->bMoveDown, SIGNAL(clicked()), this, SLOT(slotMoveDownClicked()));
53 54 55
    connect(m_child->bAdd, SIGNAL(clicked()), this, SLOT(slotAddClicked()));
    connect(m_child->bModify, SIGNAL(clicked()), this, SLOT(slotModifyClicked()));
    connect(m_child->bRemove, SIGNAL(clicked()), this, SLOT(slotRemoveClicked()));
56 57

    resize( 400, 300 );
58 59
}

60
void TagGuesserConfigDlg::slotCurrentChanged(QModelIndex item)
61
{
62
    m_child->bRemove->setEnabled(m_tagSchemeModel->rowCount() != 0);
63

64
    // Ensure up/down buttons are appropriately enabled.
65

66 67 68 69 70 71 72 73 74
    if (!m_tagSchemeModel->rowCount() || item == m_tagSchemeModel->index(0, 0, QModelIndex()))
        m_child->bMoveUp->setEnabled(false);
    else
        m_child->bMoveUp->setEnabled(true);

    if (!m_tagSchemeModel->rowCount() || item == m_tagSchemeModel->index(m_tagSchemeModel->rowCount(QModelIndex())-1, 0, QModelIndex()))
        m_child->bMoveDown->setEnabled(false);
    else
        m_child->bMoveDown->setEnabled(true);
75 76
}

77 78
void TagGuesserConfigDlg::slotMoveUpClicked()
{
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    QModelIndex currentItem = m_child->lvSchemes->currentIndex();
    int row = currentItem.row();

    m_tagSchemeModel->insertRow(row - 1); // Insert in front of item above
    row++; // Now we're one row down

    QModelIndex newItem = m_tagSchemeModel->index(row - 2, 0);

    // Copy over, then delete old item
    currentItem = m_tagSchemeModel->index(row, 0);
    m_tagSchemeModel->setData(newItem, m_tagSchemeModel->data(currentItem, Qt::DisplayRole), Qt::DisplayRole);
    m_tagSchemeModel->removeRow(row);

    m_child->lvSchemes->setCurrentIndex(newItem);
    slotCurrentChanged(newItem);
94 95 96 97
}

void TagGuesserConfigDlg::slotMoveDownClicked()
{
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    QModelIndex currentItem = m_child->lvSchemes->currentIndex();
    int row = currentItem.row();

    m_tagSchemeModel->insertRow(row + 2); // Insert in front of 2 items below

    QModelIndex newItem = m_tagSchemeModel->index(row + 2, 0);

    // Copy over, then delete old item
    currentItem = m_tagSchemeModel->index(row, 0);
    m_tagSchemeModel->setData(newItem, m_tagSchemeModel->data(currentItem, Qt::DisplayRole), Qt::DisplayRole);
    m_tagSchemeModel->removeRow(row);

    newItem = m_tagSchemeModel->index(row + 1, 0);
    m_child->lvSchemes->setCurrentIndex(newItem);
    slotCurrentChanged(newItem);
113 114
}

115 116
void TagGuesserConfigDlg::slotAddClicked()
{
117 118 119 120
    m_tagSchemeModel->insertRow(0, QModelIndex());
    m_child->lvSchemes->setCurrentIndex(m_tagSchemeModel->index(0, 0, QModelIndex()));
    m_child->lvSchemes->edit(m_child->lvSchemes->currentIndex());
    slotCurrentChanged(m_child->lvSchemes->currentIndex());
121 122 123 124
}

void TagGuesserConfigDlg::slotModifyClicked()
{
125
    m_child->lvSchemes->edit(m_child->lvSchemes->currentIndex());
126 127 128 129
}

void TagGuesserConfigDlg::slotRemoveClicked()
{
130 131
    m_tagSchemeModel->removeRow(m_child->lvSchemes->currentIndex().row(), QModelIndex());
    slotCurrentChanged(m_child->lvSchemes->currentIndex());
132 133
}

134 135 136 137 138
void TagGuesserConfigDlg::accept()
{
    TagGuesser::setSchemeStrings(m_tagSchemeModel->stringList());
    KDialog::accept();
}
139 140

// vim: set et sw=4 tw=0 sta: