tokenhelpdialog.cpp (krename-4.0.9) | : | tokenhelpdialog.cpp (krename-4.90.90) | ||
---|---|---|---|---|
skipping to change at line 23 | skipping to change at line 23 | |||
* the Free Software Foundation; either version 2 of the License, or * | * the Free Software Foundation; either version 2 of the License, or * | |||
* (at your option) any later version. * | * (at your option) any later version. * | |||
* * | * * | |||
***************************************************************************/ | ***************************************************************************/ | |||
#include "tokenhelpdialog.h" | #include "tokenhelpdialog.h" | |||
#include "batchrenamer.h" | #include "batchrenamer.h" | |||
#include "krenamemodel.h" | #include "krenamemodel.h" | |||
#include <kapplication.h> | ||||
#include <kiconloader.h> | #include <kiconloader.h> | |||
#include <kpushbutton.h> | #include <KSharedConfig> | |||
#include <KTreeWidgetSearchLine> | ||||
#include <QPushButton> | ||||
#include <KConfigGroup> | ||||
#include <QDialogButtonBox> | ||||
#include <QVBoxLayout> | ||||
#include <QLineEdit> | ||||
#define COLUMN_PREVIEW 2 | #define COLUMN_PREVIEW 2 | |||
/** | /** | |||
* A wrapper class for KRenameModel that | * A wrapper class for KRenameModel that | |||
* replaces display role with customer Qt::UserRole | * replaces display role with customer Qt::UserRole | |||
* so that the KRenameModel will always return unformatted | * so that the KRenameModel will always return unformatted | |||
* data, because QComboBox cannot display richtext. | * data, because QComboBox cannot display richtext. | |||
*/ | */ | |||
class KRenameUnformattedWrappedModel : public QAbstractListModel { | class KRenameUnformattedWrappedModel : public QAbstractListModel | |||
{ | ||||
public: | public: | |||
KRenameUnformattedWrappedModel( KRenameModel* model ) | KRenameUnformattedWrappedModel(KRenameModel *model) | |||
: m_model(model) | : m_model(model) | |||
{ | { | |||
} | } | |||
/** Get the file at position index. | /** Get the file at position index. | |||
* | * | |||
* @param a valid index in the internal vector | * @param a valid index in the internal vector | |||
* | * | |||
* @returns a KRenameFile object | * @returns a KRenameFile object | |||
*/ | */ | |||
const KRenameFile & file( int index ) const { | const KRenameFile &file(int index) const | |||
return m_model->file( index ); | { | |||
} | return m_model->file(index); | |||
} | ||||
/** Get the file at position index. | ||||
* | /** Get the file at position index. | |||
* @param a valid index in the internal vector | * | |||
* | * @param a valid index in the internal vector | |||
* @returns a KRenameFile object | * | |||
*/ | * @returns a KRenameFile object | |||
KRenameFile & file( int index ) { | */ | |||
return m_model->file( index ); | KRenameFile &file(int index) | |||
} | { | |||
return m_model->file(index); | ||||
virtual QVariant data ( const QModelIndex & index, int role = Qt::DisplayRol | } | |||
e ) const { | ||||
if( role == Qt::DisplayRole ) { | virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) | |||
return static_cast<KRenameModel*>(m_model)->data(index, Qt::UserRole | const | |||
); | { | |||
if (role == Qt::DisplayRole) { | ||||
return static_cast<KRenameModel *>(m_model)->data(index, Qt::UserRol | ||||
e); | ||||
} else { | } else { | |||
return static_cast<KRenameModel*>(m_model)->data(index, role); | return static_cast<KRenameModel *>(m_model)->data(index, role); | |||
} | } | |||
} | } | |||
virtual int rowCount ( const QModelIndex & parent = QModelIndex() ) const { | virtual int rowCount(const QModelIndex &parent = QModelIndex()) const | |||
return m_model->rowCount( parent ); | { | |||
return m_model->rowCount(parent); | ||||
} | } | |||
private: | private: | |||
KRenameModel* m_model; | KRenameModel *m_model; | |||
}; | }; | |||
const int TokenHelpDialog::S_MAX_RECENT = 10; | const int TokenHelpDialog::S_MAX_RECENT = 10; | |||
TokenHelpDialog::TokenHelpDialog( KRenameModel* model, BatchRenamer* renamer, | TokenHelpDialog::TokenHelpDialog(KRenameModel *model, BatchRenamer *renamer, | |||
QLineEdit* edit, QWidget* parent ) | QLineEdit *edit, QWidget *parent) | |||
: KDialog( parent ), m_edit( edit ), m_renamer(renamer) | : QDialog(parent), m_edit(edit), m_renamer(renamer) | |||
{ | { | |||
m_model = new KRenameUnformattedWrappedModel(model); | m_model = new KRenameUnformattedWrappedModel(model); | |||
m_widget.setupUi( mainWidget() ); | QWidget *mainWidget = new QWidget(this); | |||
QVBoxLayout *mainLayout = new QVBoxLayout; | ||||
this->setButtons( KDialog::Close | KDialog::User1 ); | setLayout(mainLayout); | |||
this->setButtonText( KDialog::User1, i18n("&Insert") ); | mainLayout->addWidget(mainWidget); | |||
m_widget.setupUi(mainWidget); | ||||
KPushButton* insert = this->button( KDialog::User1 ); | ||||
KPushButton* close = this->button( KDialog::Close ); | QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close); | |||
QPushButton *insert = new QPushButton; | ||||
m_widget.searchCategory->searchLine()->setTreeWidget( m_widget.listCategorie | buttonBox->addButton(insert, QDialogButtonBox::ActionRole); | |||
s ); | this->connect(buttonBox, &QDialogButtonBox::accepted, | |||
m_widget.searchToken ->searchLine()->setTreeWidget( m_widget.listTokens ); | this, &TokenHelpDialog::accept); | |||
m_widget.comboPreview->setModel( m_model ); | this->connect(buttonBox, &QDialogButtonBox::rejected, | |||
m_widget.listTokens->sortItems( 0, Qt::AscendingOrder ); | this, &TokenHelpDialog::reject); | |||
mainLayout->addWidget(buttonBox); | ||||
connect(insert, SIGNAL(clicked(bool)), SLOT(slotInsert())); | insert->setText(i18n("&Insert")); | |||
connect(this, SIGNAL(rejected()), SLOT(reject())); | ||||
connect(close, SIGNAL(clicked(bool)), SLOT(saveConfig())); | QPushButton *close = buttonBox->button(QDialogButtonBox::Close); | |||
connect(m_widget.listCategories, SIGNAL(itemClicked(QTreeWidgetItem*,int)), | m_widget.searchCategory->searchLine()->setTreeWidget(m_widget.listCategories | |||
this, SLOT(slotCategoryChanged(QTreeWidgetItem*))); | ); | |||
connect(m_widget.listTokens, SIGNAL(itemClicked(QTreeWidgetItem*,int)), | m_widget.searchToken ->searchLine()->setTreeWidget(m_widget.listTokens); | |||
this, SLOT(slotInsert())); | m_widget.comboPreview->setModel(m_model); | |||
connect(m_widget.checkPreview, SIGNAL(clicked(bool)), this, SLOT(slotPrevi | m_widget.listTokens->sortItems(0, Qt::AscendingOrder); | |||
ewClicked(bool))); | ||||
connect(m_widget.comboPreview, SIGNAL(activated(int)), this, SLOT(slotUpda | connect(insert, &QPushButton::clicked, | |||
tePreview())); | this, &TokenHelpDialog::slotInsert); | |||
connect(close, &QPushButton::clicked, | ||||
this, &TokenHelpDialog::saveConfig); | ||||
connect(m_widget.listCategories, &QTreeWidget::itemClicked, | ||||
this, &TokenHelpDialog::slotCategoryChanged); | ||||
connect(m_widget.listTokens, &QTreeWidget::itemClicked, | ||||
this, &TokenHelpDialog::slotInsert); | ||||
connect(m_widget.checkPreview, &QCheckBox::clicked, | ||||
this, &TokenHelpDialog::slotPreviewClicked); | ||||
connect(m_widget.comboPreview, static_cast<void (QComboBox::*)(int)>(&QCombo | ||||
Box::activated), | ||||
this, &TokenHelpDialog::slotUpdatePreview); | ||||
slotEnableControls(); | slotEnableControls(); | |||
loadConfig(); | ||||
} | } | |||
TokenHelpDialog::~TokenHelpDialog() | TokenHelpDialog::~TokenHelpDialog() | |||
{ | { | |||
delete m_model; | delete m_model; | |||
} | } | |||
const QString TokenHelpDialog::getTokenSeparator() | const QString TokenHelpDialog::getTokenSeparator() | |||
{ | { | |||
return QString::fromLatin1(";;"); | return QString::fromLatin1(";;"); | |||
} | } | |||
void TokenHelpDialog::add( const QString & headline, const QStringList & command s, const QPixmap & icon, bool first ) | void TokenHelpDialog::add(const QString &headline, const QStringList &commands, const QPixmap &icon, bool first) | |||
{ | { | |||
m_map[headline] = commands; | m_map[headline] = commands; | |||
QTreeWidgetItem* item = new QTreeWidgetItem( m_widget.listCategories ); | QTreeWidgetItem *item = new QTreeWidgetItem(m_widget.listCategories); | |||
item->setText( 0, headline ); | item->setText(0, headline); | |||
item->setIcon( 0, icon ); | item->setIcon(0, icon); | |||
if( first ) | if (first) { | |||
m_first = headline; | m_first = headline; | |||
} | ||||
} | } | |||
int TokenHelpDialog::exec() | int TokenHelpDialog::exec() | |||
{ | { | |||
loadConfig(); | ||||
addRecentTokens(); | addRecentTokens(); | |||
m_widget.listCategories->sortItems( 0, Qt::AscendingOrder ); | m_widget.listCategories->sortItems(0, Qt::AscendingOrder); | |||
if( !m_lastSelected.isEmpty() ) | if (!m_lastSelected.isEmpty()) { | |||
{ | selectCategory(m_lastSelected); | |||
selectCategory( m_lastSelected ); | } else if (!m_first.isEmpty()) { | |||
} | selectCategory(m_first); | |||
else if( !m_first.isEmpty() ) | ||||
{ | ||||
selectCategory( m_first ); | ||||
} | } | |||
return QDialog::exec(); | return QDialog::exec(); | |||
} | } | |||
void TokenHelpDialog::selectCategory( const QString & category ) | void TokenHelpDialog::selectCategory(const QString &category) | |||
{ | { | |||
for( int i=0;i<m_widget.listCategories->topLevelItemCount(); i++ ) | for (int i = 0; i < m_widget.listCategories->topLevelItemCount(); i++) | |||
if( m_widget.listCategories->topLevelItem( i )->text(0) == category ) | if (m_widget.listCategories->topLevelItem(i)->text(0) == category) { | |||
{ | m_widget.listCategories->topLevelItem(i)->setSelected(true); | |||
m_widget.listCategories->topLevelItem( i )->setSelected( true ); | this->slotCategoryChanged(m_widget.listCategories->topLevelItem(i)); | |||
this->slotCategoryChanged( m_widget.listCategories->topLevelItem( i | ||||
) ); | ||||
break; | break; | |||
} | } | |||
} | } | |||
void TokenHelpDialog::slotCategoryChanged( QTreeWidgetItem* item ) | void TokenHelpDialog::slotCategoryChanged(QTreeWidgetItem *item) | |||
{ | { | |||
m_widget.listTokens->clear(); | m_widget.listTokens->clear(); | |||
const QStringList & commands = m_map[item->text(0)]; | const QStringList &commands = m_map[item->text(0)]; | |||
for( int i=0;i<commands.count(); i++ ) | for (int i = 0; i < commands.count(); i++) { | |||
{ | QTreeWidgetItem *item = new QTreeWidgetItem(m_widget.listTokens); | |||
QTreeWidgetItem* item = new QTreeWidgetItem( m_widget.listTokens ); | item->setText(0, commands[i].section(getTokenSeparator(), 0, 0)); | |||
item->setText( 0, commands[i].section(getTokenSeparator(), 0, 0 ) ); | item->setText(1, commands[i].section(getTokenSeparator(), 1, 1)); | |||
item->setText( 1, commands[i].section(getTokenSeparator(), 1, 1 ) ); | ||||
} | } | |||
slotUpdatePreview(); | slotUpdatePreview(); | |||
} | } | |||
void TokenHelpDialog::slotInsert() | void TokenHelpDialog::slotInsert() | |||
{ | { | |||
QTreeWidgetItem* category = m_widget.listCategories->currentItem(); | QTreeWidgetItem *category = m_widget.listCategories->currentItem(); | |||
if( category ) | if (category) { | |||
m_lastSelected = category->text(0); | m_lastSelected = category->text(0); | |||
} | ||||
QTreeWidgetItem* item = m_widget.listTokens->currentItem(); | QTreeWidgetItem *item = m_widget.listTokens->currentItem(); | |||
if( item ) | if (item) { | |||
{ | const QString &token = item->text(0); | |||
const QString & token = item->text( 0 ); | const QString &help = item->text(1); | |||
const QString & help = item->text( 1 ); | ||||
addToRecentTokens( token, help ); | addToRecentTokens(token, help); | |||
saveConfig(); | saveConfig(); | |||
m_edit->insert( token ); | m_edit->insert(token); | |||
} | } | |||
this->accept(); | this->accept(); | |||
} | } | |||
void TokenHelpDialog::loadConfig() | void TokenHelpDialog::loadConfig() | |||
{ | { | |||
KSharedConfigPtr config = KGlobal::config(); | KSharedConfigPtr config = KSharedConfig::openConfig(); | |||
KConfigGroup groupGui = config->group( QString("TokenHelpDialog") ); | KConfigGroup groupGui = config->group(QString("TokenHelpDialog")); | |||
m_lastSelected = groupGui.readEntry( "LastSelectedCategory", m_lastSelected | m_lastSelected = groupGui.readEntry("LastSelectedCategory", m_lastSelected); | |||
); | m_recent = groupGui.readEntry("RecentTokens", m_recent); | |||
m_recent = groupGui.readEntry( "RecentTokens", m_recent ); | ||||
bool preview = groupGui.readEntry( "Preview", m_widget.checkPreview->isChec | bool preview = groupGui.readEntry("Preview", m_widget.checkPreview->isCheck | |||
ked() ); | ed()); | |||
m_widget.checkPreview->setChecked( preview ); | m_widget.checkPreview->setChecked(preview); | |||
int width = groupGui.readEntry( "Column0", QVariant(m_widget.listTokens->col | int width = groupGui.readEntry("Column0", QVariant(m_widget.listTokens->colu | |||
umnWidth( 0 )) ).toInt(); | mnWidth(0))).toInt(); | |||
if( width > 0 ) | if (width > 0) { | |||
m_widget.listTokens->setColumnWidth( 0, width ); | m_widget.listTokens->setColumnWidth(0, width); | |||
} | ||||
width = groupGui.readEntry( "Column1", QVariant(m_widget.listTokens->columnW | width = groupGui.readEntry("Column1", QVariant(m_widget.listTokens->columnWi | |||
idth( 1 )) ).toInt(); | dth(1))).toInt(); | |||
if( width > 0 ) | if (width > 0) { | |||
m_widget.listTokens->setColumnWidth( 1, width ); | m_widget.listTokens->setColumnWidth(1, width); | |||
} | ||||
width = groupGui.readEntry( "Column2", QVariant(m_widget.listTokens->columnW | width = groupGui.readEntry("Column2", QVariant(m_widget.listTokens->columnWi | |||
idth( 2 )) ).toInt(); | dth(2))).toInt(); | |||
if( width > 0 ) | if (width > 0) { | |||
m_widget.listTokens->setColumnWidth( 2, width ); | m_widget.listTokens->setColumnWidth(2, width); | |||
} | ||||
this->restoreDialogSize( groupGui ); | restoreGeometry(groupGui.readEntry<QByteArray>("Geometry", QByteArray())); | |||
QList<int> sizes = groupGui.readEntry( "Splitter", QList<int>() ); | QList<int> sizes = groupGui.readEntry("Splitter", QList<int>()); | |||
if( sizes.size() == 2 ) { | if (sizes.size() == 2) { | |||
m_widget.splitter->setSizes( sizes ); | m_widget.splitter->setSizes(sizes); | |||
} | } | |||
} | } | |||
void TokenHelpDialog::saveConfig() | void TokenHelpDialog::saveConfig() | |||
{ | { | |||
KSharedConfigPtr config = KGlobal::config(); | KSharedConfigPtr config = KSharedConfig::openConfig(); | |||
KConfigGroup groupGui = config->group( QString("TokenHelpDialog") ); | KConfigGroup groupGui = config->group(QString("TokenHelpDialog")); | |||
groupGui.writeEntry( "Column0", m_widget.listTokens->columnWidth( 0 ) ); | groupGui.writeEntry("Column0", m_widget.listTokens->columnWidth(0)); | |||
groupGui.writeEntry( "Column1", m_widget.listTokens->columnWidth( 1 ) ); | groupGui.writeEntry("Column1", m_widget.listTokens->columnWidth(1)); | |||
groupGui.writeEntry( "Column2", m_widget.listTokens->columnWidth( 2 ) ); | groupGui.writeEntry("Column2", m_widget.listTokens->columnWidth(2)); | |||
groupGui.writeEntry( "Splitter", m_widget.splitter->sizes() ); | groupGui.writeEntry("Splitter", m_widget.splitter->sizes()); | |||
groupGui.writeEntry( "Preview", m_widget.checkPreview->isChecked() ); | groupGui.writeEntry("Preview", m_widget.checkPreview->isChecked()); | |||
groupGui.writeEntry( "LastSelectedCategory", m_lastSelected ); | groupGui.writeEntry("LastSelectedCategory", m_lastSelected); | |||
groupGui.writeEntry( "RecentTokens", m_recent ); | groupGui.writeEntry("RecentTokens", m_recent); | |||
this->saveDialogSize( groupGui ); | groupGui.writeEntry("Geometry", saveGeometry()); | |||
} | } | |||
void TokenHelpDialog::slotEnableControls() | void TokenHelpDialog::slotEnableControls() | |||
{ | { | |||
m_widget.comboPreview->setEnabled( m_widget.checkPreview->isChecked() ); | m_widget.comboPreview->setEnabled(m_widget.checkPreview->isChecked()); | |||
m_widget.labelPreview->setEnabled( m_widget.checkPreview->isChecked() ); | m_widget.labelPreview->setEnabled(m_widget.checkPreview->isChecked()); | |||
} | } | |||
void TokenHelpDialog::slotPreviewClicked(bool bPreview) | void TokenHelpDialog::slotPreviewClicked(bool bPreview) | |||
{ | { | |||
slotEnableControls(); | slotEnableControls(); | |||
if( bPreview ) | if (bPreview) { | |||
{ | m_widget.listTokens->setColumnHidden(COLUMN_PREVIEW, false); | |||
m_widget.listTokens->setColumnHidden( COLUMN_PREVIEW, false ); | ||||
slotUpdatePreview(); | slotUpdatePreview(); | |||
// make sure preview column is visible | // make sure preview column is visible | |||
m_widget.listTokens->resizeColumnToContents( 0 ); | m_widget.listTokens->resizeColumnToContents(0); | |||
m_widget.listTokens->resizeColumnToContents( 1 ); | m_widget.listTokens->resizeColumnToContents(1); | |||
} | } else { | |||
else | m_widget.listTokens->setColumnHidden(COLUMN_PREVIEW, true); | |||
{ | ||||
m_widget.listTokens->setColumnHidden( COLUMN_PREVIEW, true ); | ||||
} | } | |||
} | } | |||
void TokenHelpDialog::slotUpdatePreview() | void TokenHelpDialog::slotUpdatePreview() | |||
{ | { | |||
if( !m_widget.checkPreview->isChecked() ) | if (!m_widget.checkPreview->isChecked()) { | |||
return; | return; | |||
} | ||||
int index = m_widget.comboPreview->currentIndex(); | int index = m_widget.comboPreview->currentIndex(); | |||
if( index >= 0 && m_widget.listCategories->currentItem() != NULL ) | if (index >= 0 && m_widget.listCategories->currentItem() != nullptr) { | |||
{ | ||||
QString name = m_widget.listCategories->currentItem()->text(0); | QString name = m_widget.listCategories->currentItem()->text(0); | |||
const KRenameFile & file = m_model->file( index ); | const KRenameFile &file = m_model->file(index); | |||
KApplication::setOverrideCursor( Qt::WaitCursor ); | QApplication::setOverrideCursor(Qt::WaitCursor); | |||
QString token; | QString token; | |||
for( int i=0;i<m_widget.listTokens->topLevelItemCount();i++ ) | for (int i = 0; i < m_widget.listTokens->topLevelItemCount(); i++) { | |||
{ | QTreeWidgetItem *item = m_widget.listTokens->topLevelItem(i); | |||
QTreeWidgetItem* item = m_widget.listTokens->topLevelItem( i ); | if (item) { | |||
if( item ) | token = m_renamer->processString(item->text(0), file.srcFilename | |||
{ | (), index); | |||
token = m_renamer->processString( item->text( 0 ), file.srcFilen | item->setText(COLUMN_PREVIEW, token); | |||
ame(), index ); | ||||
item->setText( COLUMN_PREVIEW, token ); | ||||
} | } | |||
} | } | |||
KApplication::restoreOverrideCursor(); | QApplication::restoreOverrideCursor(); | |||
} | } | |||
} | } | |||
void TokenHelpDialog::addRecentTokens() | void TokenHelpDialog::addRecentTokens() | |||
{ | { | |||
const QPixmap & icon = | const QPixmap &icon = | |||
KIconLoader::global()->loadIcon( "document-open-recent", KIconLoader::NoGrou | KIconLoader::global()->loadIcon("document-open-recent", KIconLoader::NoG | |||
p, KIconLoader::SizeSmall ); | roup, KIconLoader::SizeSmall); | |||
this->add( i18n("Recent"), m_recent, icon ); | this->add(i18n("Recent"), m_recent, icon); | |||
} | } | |||
void TokenHelpDialog::addToRecentTokens( const QString & token, const QString & | void TokenHelpDialog::addToRecentTokens(const QString &token, const QString &hel | |||
help ) | p) | |||
{ | { | |||
// 1. Check if m_recent contains token and remove it | // 1. Check if m_recent contains token and remove it | |||
QStringList::iterator it = m_recent.begin(); | QStringList::iterator it = m_recent.begin(); | |||
while( it != m_recent.end() ) | while (it != m_recent.end()) { | |||
{ | if ((*it).startsWith(token + getTokenSeparator())) { | |||
if( (*it).startsWith( token + getTokenSeparator() ) ) | m_recent.erase(it); | |||
{ | break; | |||
m_recent.erase( it ); | } | |||
break; | ||||
} | ||||
++it; | ++it; | |||
} | } | |||
// 2. remove first token if more than 10 | // 2. remove first token if more than 10 | |||
if( m_recent.count() >= S_MAX_RECENT ) | if (m_recent.count() >= S_MAX_RECENT) { | |||
{ | m_recent.removeAt(0); | |||
m_recent.removeAt( 0 ); | } | |||
} | ||||
// 3. append token | // 3. append token | |||
QString recent = token + getTokenSeparator() + help; | QString recent = token + getTokenSeparator() + help; | |||
m_recent << recent; | m_recent << recent; | |||
} | } | |||
#include "tokenhelpdialog.moc" | ||||
End of changes. 55 change blocks. | ||||
185 lines changed or deleted | 196 lines changed or added |