diff --git a/src/addalternatives.cpp b/src/addalternatives.cpp index 995ac9f..ec70485 100644 --- a/src/addalternatives.cpp +++ b/src/addalternatives.cpp @@ -1,115 +1,115 @@ /*************************************************************************** * Copyright (C) 2004 by Mario Bensi * * Copyright (C) 2004, 2008 by Pino Toscano * * * * 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, write to the * * Free Software Foundation, Inc., * * 51 Franklin Steet, Fifth Floor, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "addalternatives.h" #include "altparser.h" #include "slavewidget.h" #include #include #include #include AddAlternatives::AddAlternatives(Item* item, QWidget *parent) - : QDialog(parent), m_item(item), m_alternative(0) + : QDialog(parent), m_item(item), m_alternative(Q_NULLPTR) { QVBoxLayout* lay = new QVBoxLayout(this); QWidget* main = new QWidget(this); lay->addWidget(main); setupUi(main); main->layout()->setMargin(0); m_buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); lay->addWidget(m_buttons); setWindowTitle(i18n("Add Alternative")); m_Path->setWindowTitle( i18n( "Choose Alternative" ) ); m_Path->setFilter( i18n( "*|All Files" ) ); m_Path->setMode( KFile::File | KFile::LocalOnly ); const int slaveCount = item->getSlaves()->count(); if (slaveCount > 0) { SlaveList *slaves = item->getSlaves(); QWidget *w = new QWidget; QVBoxLayout *lay = new QVBoxLayout(w); for (int i = 0; i < slaveCount; ++i) { if (i > 0) lay->addWidget(new KSeparator(Qt::Horizontal, w)); SlaveWidget *sw = new SlaveWidget(slaves->at(i), w); lay->addWidget(sw); m_slaveWidgets.append(sw); connect(sw, SIGNAL(slaveChanged(QString)), this, SLOT(slotCheckSlaves())); } w->show(); m_slavesArea->setWidget(w); } else { m_slavesGroup->hide(); } m_buttons->button(QDialogButtonBox::Ok)->setEnabled(false); connect(m_Path, SIGNAL(textChanged(QString)), this, SLOT(slotCheckSlaves())); connect(m_buttons, SIGNAL(accepted()), this, SLOT(slotOkClicked())); connect(m_buttons, SIGNAL(rejected()), this, SLOT(reject())); } AddAlternatives::~AddAlternatives() { } QSize AddAlternatives::sizeHint() const { return QSize(400, QDialog::sizeHint().height()); } void AddAlternatives::slotCheckSlaves() { bool ok = !m_Path->url().isEmpty(); int i = 0; while ((i < m_slaveWidgets.count()) && ok) { ok = !m_slaveWidgets.at(i)->slavePath().isEmpty(); ++i; } m_buttons->button(QDialogButtonBox::Ok)->setEnabled(ok); } void AddAlternatives::slotOkClicked() { m_alternative = new Alternative(m_item); Q_ASSERT(!m_Path->url().toLocalFile().isEmpty()); m_alternative->setPath(m_Path->url().toLocalFile()); m_alternative->setPriority(m_Priority->value()); Q_FOREACH (SlaveWidget *sw, m_slaveWidgets) { Q_ASSERT(!sw->slavePath().isEmpty()); m_alternative->addSlave(sw->slavePath()); } accept(); } #include diff --git a/src/addalternatives.h b/src/addalternatives.h index 94645c0..3d90d89 100644 --- a/src/addalternatives.h +++ b/src/addalternatives.h @@ -1,57 +1,57 @@ /*************************************************************************** * Copyright (C) 2004 by Mario Bensi * * Copyright (C) 2004, 2008 by Pino Toscano * * * * 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, write to the * * Free Software Foundation, Inc., * * 51 Franklin Steet, Fifth Floor, Boston, MA 02111-1307, USA. * ***************************************************************************/ #ifndef _ADDALTERNATIVES_H_ #define _ADDALTERNATIVES_H_ #include "ui_addalternativesui.h" #include #include class QDialogButtonBox; class Alternative; class Item; class SlaveWidget; class AddAlternatives : public QDialog, private Ui::AddAlternatives { Q_OBJECT QDialogButtonBox* m_buttons; Item* m_item; Alternative* m_alternative; QList m_slaveWidgets; public: - AddAlternatives(Item* item, QWidget *parent = 0); + AddAlternatives(Item* item, QWidget *parent = Q_NULLPTR); virtual ~AddAlternatives(); virtual QSize sizeHint() const; Alternative* alternative() const { return m_alternative; } private slots: void slotCheckSlaves(); void slotOkClicked(); }; #endif //ADDALTERNATIVES_H_ diff --git a/src/alternativemodels.cpp b/src/alternativemodels.cpp index aa872f6..7be829c 100644 --- a/src/alternativemodels.cpp +++ b/src/alternativemodels.cpp @@ -1,976 +1,976 @@ /*************************************************************************** * Copyright (C) 2008-2009 by Pino Toscano * * * * 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. * ***************************************************************************/ #include "alternativemodels.h" #include "altparser.h" #include #include #include #include #include #include #include #include #include #include enum ItemChangeType { SelectionItemChange = 1, AltNumItemChange = 2, ModeItemChange = 4 }; Q_DECLARE_FLAGS(ItemChanges, ItemChangeType) Q_DECLARE_OPERATORS_FOR_FLAGS(ItemChanges) namespace { struct AltNode { enum { Type = 0 }; AltNode(AltNode *pp, int t) : parent(pp), type(t) {} virtual ~AltNode() {} virtual QList children() const { return QList(); } virtual int childCount() const { return 0; } AltNode *parent; int type : 3; }; struct AltAlternativeNode; struct AltItemNode; struct AltRootNode : public AltNode { enum { Type = 1 }; AltRootNode() - : AltNode(0, Type) {} + : AltNode(Q_NULLPTR, Type) {} virtual ~AltRootNode() { qDeleteAll(m_children); } virtual QList children() const { QList c; std::copy(m_children.begin(), m_children.end(), std::back_inserter(c)); return c; } virtual int childCount() const { return m_children.count(); } QList m_children; }; struct AltItemNode : public AltNode { enum { Type = 2 }; AltItemNode(Item *i, AltRootNode *p) : AltNode(p, Type), item(i) , changed(false), nbrAltChanged(false), modeChanged(false) {} virtual ~AltItemNode() { qDeleteAll(m_children); } virtual QList children() const { QList c; std::copy(m_children.begin(), m_children.end(), std::back_inserter(c)); return c; } virtual int childCount() const { return m_children.count(); } Item *item; QList m_children; bool changed : 1; bool nbrAltChanged : 1; bool modeChanged : 1; }; struct AltAlternativeNode : public AltNode { enum { Type = 3 }; AltAlternativeNode(Alternative *a, AltItemNode *p) : AltNode(p, Type), alternative(a), selected(false) {} Alternative *alternative; bool selected : 1; }; template T* altnode_cast(AltNode *n) { - return n->type > 0 && n->type == T::Type ? static_cast(n) : 0; + return n->type > 0 && n->type == T::Type ? static_cast(n) : Q_NULLPTR; } template <> AltNode* altnode_cast(AltNode *n) { return n; } } class AlternativesBaseModelPrivate { public: AlternativesBaseModelPrivate(); virtual ~AlternativesBaseModelPrivate(); Q_DECLARE_PUBLIC(AlternativesBaseModel) virtual void load() = 0; virtual AltNode* root() const = 0; QModelIndex indexForItem(AltNode *n, int col) const; AlternativesBaseModel *q_ptr; }; AlternativesBaseModelPrivate::AlternativesBaseModelPrivate() - : q_ptr(0) + : q_ptr(Q_NULLPTR) { } AlternativesBaseModelPrivate::~AlternativesBaseModelPrivate() { } QModelIndex AlternativesBaseModelPrivate::indexForItem(AltNode *n, int col) const { if (n->parent) { const QList children = n->parent->children(); const int id = children.indexOf(n); if (id >= 0 && id < children.count()) return q_ptr->createIndex(id, col, n); } return QModelIndex(); } AlternativesBaseModel::AlternativesBaseModel(AlternativesBaseModelPrivate &dd, QObject *parent) : QAbstractItemModel(parent), d_ptr(&dd) { d_ptr->q_ptr = this; d_ptr->load(); } AlternativesBaseModel::~AlternativesBaseModel() { delete d_ptr; } bool AlternativesBaseModel::hasChildren(const QModelIndex &parent) const { if (!parent.isValid()) return true; AltNode *n = static_cast(parent.internalPointer()); return n->childCount() > 0; } QModelIndex AlternativesBaseModel::index(int row, int column, const QModelIndex &parent) const { if (row < 0 || column < 0 || column >= columnCount(parent)) return QModelIndex(); AltNode *n = parent.isValid() ? static_cast(parent.internalPointer()) : d_ptr->root(); const QList children = n->children(); if (row < children.count()) return createIndex(row, column, children.at(row)); return QModelIndex(); } QModelIndex AlternativesBaseModel::parent(const QModelIndex &index) const { if (!index.isValid()) return QModelIndex(); AltNode *n = static_cast(index.internalPointer()); return d_ptr->indexForItem(n->parent, index.column()); } int AlternativesBaseModel::rowCount(const QModelIndex &parent) const { AltNode *n = parent.isValid() ? static_cast(parent.internalPointer()) : d_ptr->root(); return n->childCount(); } class AlternativeItemsModelPrivate : public AlternativesBaseModelPrivate { public: AlternativeItemsModelPrivate(const QString &appName); ~AlternativeItemsModelPrivate(); virtual void load(); virtual AltNode* root() const { return const_cast(&m_root); } Q_DECLARE_PUBLIC(AlternativeItemsModel) void itemChanged(AltItemNode *node, ItemChanges changes); void loadItemNode(AltItemNode *node); bool isChanged(AltItemNode *node) const; AltAlternativeNode* findSelectedAlternative(AltItemNode *node, int *index) const; AltFilesManager *altManager; AltRootNode m_root; QString m_appName; KIconLoader *iconLoader; QIcon brokenAltIcon; }; AlternativeItemsModelPrivate::AlternativeItemsModelPrivate(const QString &appName) - : AlternativesBaseModelPrivate(), altManager(0) + : AlternativesBaseModelPrivate(), altManager(Q_NULLPTR) , m_appName(appName), iconLoader(new KIconLoader(m_appName)) , brokenAltIcon(KDE::icon("alternative-broken", iconLoader)) { #if defined(DISTRO_DPKG) altManager = new AltFilesManager("/var/lib/dpkg/alternatives"); #elif defined(DISTRO_RPM_2) altManager = new AltFilesManager("/var/lib/alternatives"); #elif defined(DISTRO_RPM) altManager = new AltFilesManager("/var/lib/rpm/alternatives"); #else qCritical(KALT_LOG) << "Unsupported distribution for KAlternatives."; #endif if (altManager && !altManager->parsingOk()) { qCDebug(KALT_LOG) << altManager->getErrorMsg(); delete altManager; - altManager = 0; + altManager = Q_NULLPTR; } } AlternativeItemsModelPrivate::~AlternativeItemsModelPrivate() { delete altManager; } void AlternativeItemsModelPrivate::load() { if (!altManager) return; ItemPtrList *itemslist = altManager->getGlobalAlternativeList(); Q_FOREACH (Item *i, *itemslist) { AltItemNode *newItem = new AltItemNode(i, &m_root); m_root.m_children.append(newItem); } } void AlternativeItemsModelPrivate::itemChanged(AltItemNode *node, ItemChanges changes) { Q_Q(AlternativeItemsModel); if (changes & SelectionItemChange) { node->changed = true; } if (changes & AltNumItemChange) { node->nbrAltChanged = true; } if (changes & ModeItemChange) { node->modeChanged = true; } const QModelIndex index = indexForItem(node, 0); emit q->dataChanged(index, index); } void AlternativeItemsModelPrivate::loadItemNode(AltItemNode *node) { if (!node->m_children.isEmpty()) return; AltsPtrList *alts = node->item->getAlternatives(); Q_FOREACH (Alternative* a, *alts) { AltAlternativeNode *altnode = new AltAlternativeNode(a, node); node->m_children.append(altnode); altnode->selected = a->isSelected(); } } bool AlternativeItemsModelPrivate::isChanged(AltItemNode *node) const { return node->changed || node->nbrAltChanged || node->modeChanged; } AltAlternativeNode* AlternativeItemsModelPrivate::findSelectedAlternative(AltItemNode *node, int *index) const { const int num = node->m_children.count(); for (int i = 0; i < num; ++i) { AltAlternativeNode *altnode = node->m_children.at(i); if (altnode->selected) { *index = i; return altnode; } } *index = -1; - return 0; + return Q_NULLPTR; } AlternativeItemsModel::AlternativeItemsModel(const QString &appName, QObject *parent) : AlternativesBaseModel(*new AlternativeItemsModelPrivate(appName), parent) { Q_D(AlternativeItemsModel); d->iconLoader->setParent(this); } AlternativeItemsModel::~AlternativeItemsModel() { } int AlternativeItemsModel::columnCount(const QModelIndex &parent) const { Q_UNUSED(parent) return 1; } QVariant AlternativeItemsModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); Q_D(const AlternativeItemsModel); AltNode *n = static_cast(index.internalPointer()); if (AltItemNode *n_i = altnode_cast(n)) { switch (role) { case Qt::DisplayRole: return n_i->item->getName(); case Qt::ToolTipRole: { QString tip = n_i->item->getName(); if (n_i->item->isBroken()) { tip += "\n\n"; tip += i18n("Broken alternative group."); } return tip; } case Qt::ForegroundRole: if (d->isChanged(n_i)) return QColor(Qt::red); break; case Qt::FontRole: if (d->isChanged(n_i)) { QFont f; f.setBold(true); return f; } break; case Qt::DecorationRole: if (n_i->item->isBroken()) return d->brokenAltIcon; break; case AltItemRole: return QVariant::fromValue(n_i->item); } } return QVariant(); } Qt::ItemFlags AlternativeItemsModel::flags(const QModelIndex &index) const { if (!index.isValid()) return Qt::NoItemFlags; AltNode *n = static_cast(index.internalPointer()); if (AltItemNode *n_i = altnode_cast(n)) { Qt::ItemFlags f = Qt::ItemIsSelectable; if (!n_i->item->isBroken()) f |= Qt::ItemIsEnabled; return f; } return Qt::NoItemFlags; } bool AlternativeItemsModel::hasChildren(const QModelIndex &parent) const { return parent.isValid() ? false : AlternativesBaseModel::hasChildren(parent); } QVariant AlternativeItemsModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation != Qt::Horizontal) return QVariant(); switch (role) { case Qt::DisplayRole: if (section == 0) return i18nc("Groups of alternatives", "Groups"); break; } return QVariant(); } QModelIndex AlternativeItemsModel::index(int row, int column, const QModelIndex &parent) const { return parent.isValid() ? QModelIndex() : AlternativesBaseModel::index(row, column, parent); } int AlternativeItemsModel::rowCount(const QModelIndex &parent) const { return parent.isValid() ? 0 : AlternativesBaseModel::rowCount(parent); } void AlternativeItemsModel::save() { Q_D(AlternativeItemsModel); if (!d->altManager) return; QModelIndexList changedIndexes; const int rows = d->m_root.m_children.count(); for (int i = 0; i < rows; ++i) { AltItemNode *node = d->m_root.m_children.at(i); Item *item = node->item; bool itemChanged = false; if (node->changed) { int index = 0; AltAlternativeNode* altnode = d->findSelectedAlternative(node, &index); Q_ASSERT(altnode); QString selectError; if (!altnode->alternative->select(&selectError)) { qCDebug(KALT_LOG) << selectError << endl; return; } node->changed = false; itemChanged = true; } if (node->nbrAltChanged || node->modeChanged) { QString parentPath = d->altManager->getAltDir() + '/' + item->getName(); QFile origFile(parentPath); if (origFile.exists()) { origFile.remove(); } if (origFile.open(QIODevice::WriteOnly)) { QTextStream stream(&origFile); stream << Item::modeString(item->getMode()) << endl; stream << item->getPath() << endl; SlaveList *slaveList = item->getSlaves(); Q_FOREACH (Slave *slave, *slaveList) { stream << slave->slname << endl; stream << slave->slpath << endl; } stream << endl; AltsPtrList *altItemList = item->getAlternatives(); Q_FOREACH (Alternative *a, *altItemList) { stream << a->getPath() << endl; stream << a->getPriority() << endl; Q_FOREACH (const QString &slave, a->getSlaves()) { stream << slave << endl; } } stream << endl; origFile.close(); } node->nbrAltChanged = false; node->modeChanged = false; itemChanged = true; } if (itemChanged) { changedIndexes.append(createIndex(i, 0, node)); } } Q_FOREACH (const QModelIndex &index, changedIndexes) { emit dataChanged(index, index); } } bool AlternativeItemsModel::isSupported() const { Q_D(const AlternativeItemsModel); return d->altManager; } class AlternativeAltModelPrivate : public AlternativesBaseModelPrivate { public: AlternativeAltModelPrivate(AlternativeItemsModel *itemModel, bool readOnly); ~AlternativeAltModelPrivate(); virtual void load(); virtual AltNode* root() const { return m_root; } Q_DECLARE_PUBLIC(AlternativeAltModel) AltAlternativeNode* findHigherPriority(int *index) const; void searchDescription(Alternative *alternative) const; void statusChanged(int index); AlternativeItemsModelPrivate *parentModel; AltItemNode m_nullRoot; AltItemNode *m_root; bool m_readOnly; }; AlternativeAltModelPrivate::AlternativeAltModelPrivate(AlternativeItemsModel *itemModel, bool readOnly) : AlternativesBaseModelPrivate() - , parentModel(itemModel->d_func()), m_nullRoot(0, 0), m_root(&m_nullRoot), m_readOnly(readOnly) + , parentModel(itemModel->d_func()), m_nullRoot(Q_NULLPTR, 0), m_root(&m_nullRoot), m_readOnly(readOnly) { } AlternativeAltModelPrivate::~AlternativeAltModelPrivate() { } void AlternativeAltModelPrivate::load() { } AltAlternativeNode* AlternativeAltModelPrivate::findHigherPriority(int *index) const { const int num = m_root->m_children.count(); if (!num) { *index = 0; - return 0; + return Q_NULLPTR; } int id = 0; AltAlternativeNode* n = m_root->m_children.at(id); int priority = n->alternative->getPriority(); for (int i = 1; i < num; ++i) { AltAlternativeNode* tmp = m_root->m_children.at(i); if (tmp->alternative->getPriority() > priority) { id = i; n = tmp; priority = n->alternative->getPriority(); } } *index = id; return n; } static bool extractDescriptionFor(const QString &outputLine, const QString &name, QString *desc) { QString output = outputLine; int pos = output.indexOf('('); // look for the name of the search result, and discard it // in case it is not exactly what we requested if (pos == -1 || (output.left(pos -1) != name)) return false; pos = output.indexOf(']'); if (pos != -1) { output.remove(0, pos + 1); } pos = output.indexOf(')'); if (pos != -1) { output.remove(0, pos + 1); } pos = output.indexOf('-'); if (pos != -1) { output.remove(0, pos + 2); } *desc = output; return true; } void AlternativeAltModelPrivate::searchDescription(Alternative *alternative) const { QString exec = alternative->getPath(); const int slashPos = exec.lastIndexOf('/'); if (slashPos != -1) exec.remove(0, slashPos + 1); KProcess proc; proc.setProgram("whatis", QStringList() << exec); proc.setOutputChannelMode(KProcess::SeparateChannels); proc.setEnv("COLUMNS", QString::number(300)); proc.start(); proc.waitForStarted(); proc.waitForFinished(); if (proc.exitCode() == 0) { const QByteArray procOutput = proc.readAllStandardOutput(); const QStringList outputLines = QString::fromLocal8Bit(procOutput.constData()).split('\n', QString::SkipEmptyParts); Q_FOREACH (const QString &outLine, outputLines) { QString description; if (extractDescriptionFor(outLine, exec, &description)) { alternative->setDescription(description); break; } } } } void AlternativeAltModelPrivate::statusChanged(int index) { if (m_root == &m_nullRoot) return; Q_Q(AlternativeAltModel); - QComboBox *combo = q->sender() ? qobject_cast(q->sender()) : 0; + QComboBox *combo = q->sender() ? qobject_cast(q->sender()) : Q_NULLPTR; if (!combo) return; const Item::ItemMode mode = static_cast(combo->itemData(index).toInt()); m_root->item->setMode(mode); ItemChanges changes = ModeItemChange; if (mode == Item::AutoMode) { int selectedIndex = 0; int higherPriorityIndex = 0; AltAlternativeNode *selectedaltnode = parentModel->findSelectedAlternative(m_root, &selectedIndex); AltAlternativeNode *newaltnode = findHigherPriority(&higherPriorityIndex); if (selectedIndex != higherPriorityIndex) { QModelIndexList indexes; selectedaltnode->selected = false; indexes.append(q->createIndex(selectedIndex, 0, selectedaltnode)); newaltnode->selected = true; indexes.append(q->createIndex(higherPriorityIndex, 0, newaltnode)); m_root->changed = true; Q_FOREACH (const QModelIndex &index, indexes) { emit q->dataChanged(index, index); } changes |= SelectionItemChange; } } parentModel->itemChanged(m_root, changes); } AlternativeAltModel::AlternativeAltModel(AlternativeItemsModel *itemModel, bool readOnly, QObject *parent) : AlternativesBaseModel(*new AlternativeAltModelPrivate(itemModel, readOnly), parent) { } AlternativeAltModel::~AlternativeAltModel() { } int AlternativeAltModel::columnCount(const QModelIndex &parent) const { Q_UNUSED(parent) return 3; } QVariant AlternativeAltModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); Q_D(const AlternativeAltModel); AltNode *n = static_cast(index.internalPointer()); if (AltAlternativeNode *n_a = altnode_cast(n)) { switch (role) { case Qt::DisplayRole: { switch (index.column()) { case 0: return n_a->alternative->getPath(); case 1: return n_a->alternative->getPriority(); case 2: if (n_a->alternative->getDescription().isEmpty()) d->searchDescription(n_a->alternative); return Alternative::prettyDescription(n_a->alternative); } break; } case Qt::ToolTipRole: { if (n_a->alternative->getDescription().isEmpty()) d->searchDescription(n_a->alternative); KLocalizedString tip = n_a->alternative->isBroken() ? ki18nc("%1 is the alternative path, %2 its description", "%1\n(broken)\n\n%2") : ki18nc("%1 is the alternative path, %2 its description", "%1\n\n%2"); return tip.subs(n_a->alternative->getPath()) .subs(Alternative::prettyDescription(n_a->alternative)) .toString(); } case Qt::EditRole: if (index.column() == 0) return n_a->selected; break; case Qt::CheckStateRole: if (index.column() == 0) return n_a->selected ? Qt::Checked : Qt::Unchecked; break; case Qt::DecorationRole: if (index.column() == 0 && n_a->alternative->isBroken()) return d->parentModel->brokenAltIcon; break; case AltAlternativeRole: return QVariant::fromValue(n_a->alternative); } } return QVariant(); } Qt::ItemFlags AlternativeAltModel::flags(const QModelIndex &index) const { if (!index.isValid()) return Qt::NoItemFlags; Q_D(const AlternativeAltModel); AltNode *n = static_cast(index.internalPointer()); if (AltAlternativeNode *n_a = altnode_cast(n)) { Qt::ItemFlags f = Qt::ItemIsSelectable; if (!n_a->alternative->isBroken()) f |= Qt::ItemIsEnabled; switch (index.column()) { case 0: return f | Qt::ItemIsUserCheckable; default: return f; } } return Qt::NoItemFlags; } QVariant AlternativeAltModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation != Qt::Horizontal) return QVariant(); switch (role) { case Qt::DisplayRole: switch (section) { case 0: return i18n("Option"); case 1: return i18n("Priority"); case 2: return i18n("Description"); } break; } return QVariant(); } QModelIndex AlternativeAltModel::parent(const QModelIndex &index) const { Q_UNUSED(index); return QModelIndex(); } bool AlternativeAltModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (!index.isValid()) return false; AltAlternativeNode *n = altnode_cast(static_cast(index.internalPointer())); if (!n) return false; Q_D(AlternativeAltModel); switch (role) { case Qt::CheckStateRole: { if (d->m_readOnly || n->alternative->isBroken()) break; const bool newValue = value.toBool(); if (newValue) { if (!n->selected) { QModelIndexList changedIndexes; Q_FOREACH (AltAlternativeNode* node, d->m_root->m_children) { if (node->selected) { node->selected = false; changedIndexes.append(d->indexForItem(node, index.column())); } } n->selected = true; changedIndexes.append(index); Q_FOREACH (const QModelIndex &changedIndex, changedIndexes) { emit dataChanged(changedIndex, changedIndex); } ItemChanges changes = SelectionItemChange; // when changing option, set the alternative to "manual" mode if (d->m_root->item->getMode() == Item::AutoMode) { d->m_root->item->setMode(Item::ManualMode); changes |= ModeItemChange; } d->parentModel->itemChanged(d->m_root, changes); return true; } } break; } } return false; } void AlternativeAltModel::setItem(Item *item) { Q_D(AlternativeAltModel); beginResetModel(); d->m_root = &d->m_nullRoot; Q_FOREACH (AltItemNode *n, d->parentModel->m_root.m_children) { if (n->item == item) { d->m_root = n; break; } } if (d->m_root->item) { d->parentModel->loadItemNode(d->m_root); } endResetModel(); } void AlternativeAltModel::addAlternative(Alternative *alt) { if (!alt) return; Q_D(AlternativeAltModel); if (alt->getParent() != d->m_root->item) return; const int childCount = d->m_root->m_children.count(); beginInsertRows(QModelIndex(), childCount, childCount); d->m_root->item->addAlternative(alt); d->m_root->m_children.append(new AltAlternativeNode(alt, d->m_root)); endInsertRows(); d->parentModel->itemChanged(d->m_root, AltNumItemChange); } void AlternativeAltModel::removeAlternative(Alternative *alt) { if (!alt) return; Q_D(AlternativeAltModel); if (alt->getParent() != d->m_root->item) return; int altId = 0; const int childCount = d->m_root->m_children.count(); for ( ; altId < childCount; ++altId) { if (d->m_root->m_children.at(altId)->alternative == alt) break; } if (altId == childCount) return; const bool wasSelected = d->m_root->m_children.at(altId)->selected; beginRemoveRows(QModelIndex(), altId, altId); d->m_root->item->delAlternativeByPath(alt->getPath()); delete d->m_root->m_children.at(altId); d->m_root->m_children.removeAt(altId); endRemoveRows(); if (wasSelected && !d->m_root->m_children.isEmpty()) { int row = 0; AltAlternativeNode *node = d->findHigherPriority(&row); node->selected = true; const QModelIndex changedIndex = createIndex(row, 0, node); emit dataChanged(changedIndex, changedIndex); } d->parentModel->itemChanged(d->m_root, AltNumItemChange); } AlternativeItemProxyModel::AlternativeItemProxyModel(QObject *parent) : QSortFilterProxyModel(parent), m_showSingle(false) { } AlternativeItemProxyModel::~AlternativeItemProxyModel() { } void AlternativeItemProxyModel::setShowSingleAlternative(bool show) { if (show == m_showSingle) return; m_showSingle = show; invalidateFilter(); } bool AlternativeItemProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const { Item *item = sourceModel()->index(source_row, 0, source_parent).data(AltItemRole).value(); return !item || m_showSingle || item->countAlternatives() > 1; } #include diff --git a/src/alternativemodels.h b/src/alternativemodels.h index 8228ef2..d7271e6 100644 --- a/src/alternativemodels.h +++ b/src/alternativemodels.h @@ -1,118 +1,118 @@ /*************************************************************************** * Copyright (C) 2008 by Pino Toscano * * * * 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. * ***************************************************************************/ #ifndef _ALTERNATIVEMODELS_H_ #define _ALTERNATIVEMODELS_H_ #include #include class Alternative; class AltFilesManager; class Item; class AlternativesBaseModelPrivate; class AlternativeItemsModelPrivate; class AlternativeAltModelPrivate; enum { AltItemRole = 0x00ff0001, AltAlternativeRole = 0x00ff0002 }; class AlternativesBaseModel : public QAbstractItemModel { Q_OBJECT public: ~AlternativesBaseModel(); // QAbstractItemModel interface bool hasChildren(const QModelIndex &parent = QModelIndex()) const; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; QModelIndex parent(const QModelIndex &index) const; int rowCount(const QModelIndex &parent = QModelIndex()) const; protected: AlternativesBaseModel(AlternativesBaseModelPrivate &dd, QObject *parent); Q_DECLARE_PRIVATE(AlternativesBaseModel) AlternativesBaseModelPrivate *d_ptr; }; class AlternativeItemsModel : public AlternativesBaseModel { Q_OBJECT friend class AlternativeAltModelPrivate; public: - AlternativeItemsModel(const QString &appName, QObject *parent = 0); + AlternativeItemsModel(const QString &appName, QObject *parent = Q_NULLPTR); ~AlternativeItemsModel(); // QAbstractItemModel interface int columnCount(const QModelIndex &parent = QModelIndex()) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; Qt::ItemFlags flags(const QModelIndex &index) const; bool hasChildren(const QModelIndex &parent = QModelIndex()) const; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; int rowCount(const QModelIndex &parent = QModelIndex()) const; void save(); bool isSupported() const; private: Q_DECLARE_PRIVATE(AlternativeItemsModel) }; class AlternativeAltModel : public AlternativesBaseModel { Q_OBJECT public: - AlternativeAltModel(AlternativeItemsModel *itemModel, bool readOnly, QObject *parent = 0); + AlternativeAltModel(AlternativeItemsModel *itemModel, bool readOnly, QObject *parent = Q_NULLPTR); ~AlternativeAltModel(); // QAbstractItemModel interface int columnCount(const QModelIndex &parent = QModelIndex()) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; Qt::ItemFlags flags(const QModelIndex &index) const; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; QModelIndex parent(const QModelIndex &index) const; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); void setItem(Item *item); void addAlternative(Alternative *alt); void removeAlternative(Alternative *alt); private: Q_DECLARE_PRIVATE(AlternativeAltModel) Q_PRIVATE_SLOT(d_func(), void statusChanged(int)) }; class AlternativeItemProxyModel : public QSortFilterProxyModel { Q_OBJECT public: - AlternativeItemProxyModel(QObject *parent = 0); + AlternativeItemProxyModel(QObject *parent = Q_NULLPTR); ~AlternativeItemProxyModel(); void setShowSingleAlternative(bool show); protected: bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const; private: bool m_showSingle; }; Q_DECLARE_METATYPE(Alternative*) Q_DECLARE_METATYPE(Item*) #endif diff --git a/src/altparser.h b/src/altparser.h index bbf03c1..f233e94 100644 --- a/src/altparser.h +++ b/src/altparser.h @@ -1,140 +1,140 @@ /*************************************************************************** * Copyright (C) 2004 by Juanjo Álvarez Martinez * * Copyright (C) 2008-2009 by Pino Toscano * * * * 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, write to the * * Free Software Foundation, Inc., * * 51 Franklin Steet, Fifth Floor, Boston, MA 02111-1307, USA. * ***************************************************************************/ #ifndef _ALTPARSER_H_ #define _ALTPARSER_H_ #include #include class Item; struct Slave { QString slname; QString slpath; }; class Alternative { QString m_altPath; int m_priority; QString m_description; Item *m_parent; QStringList m_altSlaves; public: Alternative(Item *parentarg); Alternative(const Alternative &alt); ~Alternative(); Alternative& operator=(const Alternative &alt); Item* getParent() const { return m_parent; } QString getPath() const { return m_altPath; } void setPath(const QString &patharg) { m_altPath = patharg; } int getPriority() const { return m_priority; } void setDescription(const QString &desc) { m_description = desc; } QString getDescription() const { return m_description; } void setPriority(int priorityarg) { m_priority = priorityarg; } QStringList getSlaves() const { return m_altSlaves; } void setSlaves(const QStringList &slaves); void addSlave(const QString &slave) { m_altSlaves.append(slave); } int slavesCount() const { return m_altSlaves.count(); } QString getSlave(int pos) const { return m_altSlaves.at(pos); } bool isSelected() const; bool isBroken() const; - bool select(QString *selectError = 0); + bool select(QString *selectError = Q_NULLPTR); static QString prettyDescription(Alternative *); }; typedef QList SlaveList; typedef QList AltsPtrList; class Item { public: enum ItemMode { AutoMode, ManualMode }; private: QString m_name; ItemMode m_mode; QString m_path; SlaveList *m_itemSlaves; AltsPtrList *m_itemAlts; public: Item(); // Deep copy constructor: Item(const Item &item); ~Item(); Item& operator=(const Item &item); Alternative* getSelected() const; QString getName() const { return m_name; } void setName(const QString &namearg) { m_name = namearg; } ItemMode getMode() const { return m_mode; } void setMode(ItemMode modearg) { m_mode = modearg; } QString getPath() const { return m_path; } void setPath(const QString &patharg) { m_path = patharg; } SlaveList *getSlaves() const { return m_itemSlaves; } void setSlaves(SlaveList *slaves); void addSlave(const QString &namearg, const QString &patharg); void delSlave(const QString &namearg); void delSlaveByPath(const QString &patharg); AltsPtrList *getAlternatives() const { return m_itemAlts; } Alternative *getAlternative(const QString &altpath); void setAlternatives(AltsPtrList &alts); int countAlternatives() const { return m_itemAlts->count(); } void delAlternativeByPath(const QString &patharg); void delAlternativeByPriority(int priorityarg); void addAlternative(Alternative *altarg) { m_itemAlts->append(altarg); } bool isBroken() const; static QString modeString(ItemMode mode); }; typedef QList ItemPtrList; class AltFilesManager { ItemPtrList *m_itemlist; QString m_altdir; QString m_errorMsg; bool m_parseOk; bool parseAltFiles(QString &errorstr); public: AltFilesManager(const QString &altdir); ~AltFilesManager(); ItemPtrList* getGlobalAlternativeList() const { return this->m_itemlist; } bool parsingOk() const { return m_parseOk; } QString getErrorMsg() const { return m_errorMsg; } Item* getItem (const QString &name) const; //FIXME: Put in a #ifdef void debugPrintAlts() const; QString getAltDir() { return m_altdir ;} }; Q_DECLARE_METATYPE(Item::ItemMode) #endif // _KALTERNATIVES_H_ diff --git a/src/kalternatives.h b/src/kalternatives.h index 49b441e..9dfed31 100644 --- a/src/kalternatives.h +++ b/src/kalternatives.h @@ -1,63 +1,63 @@ /*************************************************************************** * Copyright (C) 2004 by Juanjo Álvarez Martinez * * Copyright (C) 2004 by Mario Bensi * * Copyright (C) 2008 by Pino Toscano * * * * 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, write to the * * Free Software Foundation, Inc., * * 51 Franklin Steet, Fifth Floor, Boston, MA 02111-1307, USA. * ***************************************************************************/ #ifndef _KALTERNATIVES_H_ #define _KALTERNATIVES_H_ #include #include "ui_mainwindow.h" class AlternativeItemProxyModel; class AlternativeAltModel; class Kalternatives : public KCModule { Q_OBJECT bool m_bisRoot; Ui::MainWindow m_ui; AlternativeItemProxyModel* m_itemProxyModel; AlternativeAltModel* m_altModel; public: - Kalternatives(QWidget *parent=0, const QVariantList& = QVariantList() ); + Kalternatives(QWidget *parent=Q_NULLPTR, const QVariantList& = QVariantList() ); virtual ~Kalternatives(); bool isBisRoot() const {return m_bisRoot;} virtual void load(); virtual void save(); virtual QString quickHelp() const; public slots: void configChanged(); private slots: void slotSelectAlternativesActivated(const QModelIndex &); void slotHideAlternativesClicked(); void slotAddClicked(); void slotDeleteClicked(); void slotPropertiesClicked(); void slotUpdateStatusCombo(); void slotUpdateButtons(); }; #endif // _KALTERNATIVES_H_ diff --git a/src/slavemodel.cpp b/src/slavemodel.cpp index e475ff6..00f03e8 100644 --- a/src/slavemodel.cpp +++ b/src/slavemodel.cpp @@ -1,115 +1,115 @@ /*************************************************************************** * Copyright (C) 2008 by Pino Toscano * * * * 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. * ***************************************************************************/ #include "slavemodel.h" #include "altparser.h" #include SlaveModel::SlaveModel(QObject *parent) : QAbstractItemModel(parent) - , m_item(0), m_alt(0) + , m_item(Q_NULLPTR), m_alt(Q_NULLPTR) { } SlaveModel::~SlaveModel() { } int SlaveModel::columnCount(const QModelIndex &parent) const { return parent.isValid() ? 0 : 2; } QVariant SlaveModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || !m_item) return QVariant(); switch (role) { case Qt::DisplayRole: case Qt::ToolTipRole: if (index.column() == 0) return m_item->getSlaves()->at(index.row())->slname; else if (index.column() == 1) return m_alt ? m_alt->getSlaves().at(index.row()) : QString(); break; } return QVariant(); } bool SlaveModel::hasChildren(const QModelIndex &parent) const { return rowCount(parent) > 0; } QVariant SlaveModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation == Qt::Horizontal) { switch (role) { case Qt::DisplayRole: if (section == 0) return i18nc("Slave name", "Name"); else if (section == 1) return i18nc("Slave path", "Path"); break; } } else { if (role == Qt::DisplayRole) return section; } return QVariant(); } QModelIndex SlaveModel::index(int row, int column, const QModelIndex &parent) const { if (parent.isValid() || !m_item || column < 0 || column >= 2 || row < 0 || (row >= m_item->getSlaves()->count())) return QModelIndex(); return createIndex(row, column); } QModelIndex SlaveModel::parent(const QModelIndex &) const { return QModelIndex(); } int SlaveModel::rowCount(const QModelIndex &parent) const { return parent.isValid() ? 0 : (m_item ? m_item->getSlaves()->count(): 0); } void SlaveModel::setItem(Item *item) { if (item == m_item) return; beginResetModel(); m_item = item; - m_alt = 0; + m_alt = Q_NULLPTR; endResetModel(); } void SlaveModel::setAlternative(Alternative *alt) { if (!alt || !m_item || (alt->getParent() != m_item) || (alt->slavesCount() != m_item->getSlaves()->count())) return; m_alt = alt; emit dataChanged(createIndex(0, 1), createIndex(m_alt->slavesCount() - 1, 1)); } #include diff --git a/src/slavemodel.h b/src/slavemodel.h index 4992a6e..d688561 100644 --- a/src/slavemodel.h +++ b/src/slavemodel.h @@ -1,43 +1,43 @@ /*************************************************************************** * Copyright (C) 2008 by Pino Toscano * * * * 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. * ***************************************************************************/ #ifndef SLAVEMODEL_H #define SLAVEMODEL_H #include class Alternative; class Item; class SlaveModel : public QAbstractItemModel { Q_OBJECT public: - SlaveModel(QObject *parent = 0); + SlaveModel(QObject *parent = Q_NULLPTR); ~SlaveModel(); // QAbstractItemModel interface int columnCount(const QModelIndex &parent = QModelIndex()) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; bool hasChildren(const QModelIndex &parent = QModelIndex()) const; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; QModelIndex parent(const QModelIndex &index) const; int rowCount(const QModelIndex &parent = QModelIndex()) const; void setItem(Item *item); void setAlternative(Alternative *alt); protected: Item *m_item; Alternative *m_alt; }; #endif diff --git a/src/slavewidget.h b/src/slavewidget.h index 64191ea..8c27f77 100644 --- a/src/slavewidget.h +++ b/src/slavewidget.h @@ -1,37 +1,37 @@ /*************************************************************************** * Copyright (C) 2008 by Pino Toscano * * * * 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. * ***************************************************************************/ #ifndef SLAVEWIDGET_H #define SLAVEWIDGET_H #include "ui_slavewidget.h" struct Slave; class SlaveWidget : public QWidget, private Ui::SlaveWidgetUi { Q_OBJECT public: - explicit SlaveWidget(Slave *slave, QWidget *parent = 0); + explicit SlaveWidget(Slave *slave, QWidget *parent = Q_NULLPTR); ~SlaveWidget(); QString slavePath() const; signals: void slaveChanged(const QString &); private slots: void slotTextChanged(const QString &); private: Slave *m_slave; }; #endif