File indexing completed on 2024-04-21 04:35:57

0001 /***************************************************************************
0002 *   Copyright 2007 Niko Sams <niko.sams@gmail.com>                        *
0003 *                                                                         *
0004 *   This program is free software; you can redistribute it and/or modify  *
0005 *   it under the terms of the GNU General Public License as published by  *
0006 *   the Free Software Foundation; either version 2 of the License, or     *
0007 *   (at your option) any later version.                                   *
0008 *                                                                         *
0009 ***************************************************************************/
0010 #include "allprofilesmodel.h"
0011 #include "uploadprofilemodel.h"
0012 #include "kdevuploadplugin.h"
0013 
0014 #include <interfaces/iproject.h>
0015 #include <project/projectmodel.h>
0016 #include <interfaces/iprojectcontroller.h>
0017 
0018 #include <kconfiggroup.h>
0019 #include <kfileitem.h>
0020 
0021 AllProfilesModel::AllProfilesModel(UploadPlugin* plugin, QObject *parent)
0022     : QAbstractListModel(parent), m_plugin(plugin)
0023 {
0024 }
0025 
0026 AllProfilesModel::~AllProfilesModel()
0027 {
0028 }
0029 
0030 QVariant AllProfilesModel::data(const QModelIndex & index, int role) const
0031 {
0032     if (!index.isValid() || index.parent().isValid()) return QVariant();
0033     int rowOffset = 0;
0034     Q_FOREACH (UploadProfileModel* model, m_sourceModels) {
0035         int rowCount = model->rowCount(index.parent());
0036         if (index.row() - rowOffset < rowCount) {
0037             QVariant ret =  model->data(model->index(index.row() - rowOffset, index.column()), role);
0038             if (role == Qt::DisplayRole) {
0039                 ret = model->project()->name() + ": " + ret.toString();
0040             }
0041             return ret;
0042         }
0043         rowOffset += rowCount;
0044     }
0045     return QVariant();
0046 }
0047 
0048 int AllProfilesModel::rowCount(const QModelIndex & parent) const
0049 {
0050     if (parent.isValid()) return 0;
0051     int ret = 0;
0052     Q_FOREACH (UploadProfileModel* model, m_sourceModels) {
0053         ret += model->rowCount();
0054     }
0055     return ret;
0056 }
0057 
0058 void AllProfilesModel::addModel(UploadProfileModel* model)
0059 {
0060     beginResetModel();
0061     
0062     connect(model, SIGNAL(modelReset()), this, SLOT(sourceReset()));
0063     connect(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
0064             this, SLOT(sourceDataChanged(QModelIndex, QModelIndex)));
0065     connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)),
0066             this, SLOT(sourceRowsAboutToBeInserted(QModelIndex, int, int)));
0067     connect(model, SIGNAL(rowsInserted(QModelIndex, int, int)),
0068             this, SLOT(sourceRowsInserted()));
0069     connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)),
0070             this, SLOT(sourceRowsAboutToBeRemoved(QModelIndex, int, int)));
0071     connect(model, SIGNAL(rowsRemoved(QModelIndex, int, int)),
0072             this, SLOT(sourceRowsRemoved()));
0073 
0074     m_sourceModels.append(model);
0075     endResetModel();
0076 }
0077 
0078 void AllProfilesModel::removeModel(UploadProfileModel* model)
0079 {
0080     beginResetModel();
0081     m_sourceModels.removeAt(m_sourceModels.indexOf(model));
0082     endResetModel();
0083 }
0084 
0085 void AllProfilesModel::sourceReset()
0086 {
0087     beginResetModel();
0088     endResetModel();
0089 }
0090 
0091 void AllProfilesModel::sourceDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
0092 {
0093     if (topLeft.parent().isValid() || bottomRight.parent().isValid()) return;
0094 
0095     UploadProfileModel* sourceModel = qobject_cast<UploadProfileModel*>(sender());
0096     int rowOffset = 0;
0097     Q_FOREACH (UploadProfileModel* model, m_sourceModels) {
0098         if (model == sourceModel) {
0099             emit dataChanged(model->index(topLeft.row()+rowOffset, topLeft.column()),
0100                              model->index(bottomRight.row()+rowOffset, bottomRight.column()));
0101         }
0102         rowOffset += model->rowCount();
0103     }
0104 }
0105 
0106 void AllProfilesModel::sourceRowsAboutToBeInserted(const QModelIndex& parent, int start, int end)
0107 {
0108     if (parent.isValid()) return;
0109     UploadProfileModel* sourceModel = qobject_cast<UploadProfileModel*>(sender());
0110     int rowOffset = 0;
0111     Q_FOREACH (UploadProfileModel* model, m_sourceModels) {
0112         if (model == sourceModel) {
0113             emit beginInsertRows(parent, start+rowOffset, end+rowOffset);
0114         }
0115         rowOffset += model->rowCount();
0116     }
0117 }
0118 
0119 void AllProfilesModel::sourceRowsInserted()
0120 {
0121     emit endInsertRows();
0122 }
0123 
0124 void AllProfilesModel::sourceRowsAboutToBeRemoved(const QModelIndex& parent, int start, int end)
0125 {
0126     if (parent.isValid()) return;
0127     UploadProfileModel* sourceModel = qobject_cast<UploadProfileModel*>(sender());
0128     int rowOffset = 0;
0129     Q_FOREACH (UploadProfileModel* model, m_sourceModels) {
0130         if (model == sourceModel) {
0131             emit beginRemoveRows(parent, start+rowOffset, end+rowOffset);
0132         }
0133         rowOffset += model->rowCount();
0134     }
0135 }
0136 void AllProfilesModel::sourceRowsRemoved()
0137 {
0138     emit endRemoveRows();
0139 }
0140 
0141 UploadProfileItem* AllProfilesModel::uploadItem(const QModelIndex& index) const
0142 {
0143     if (!index.isValid() || index.parent().isValid()) return nullptr;
0144     int rowOffset = 0;
0145     Q_FOREACH (UploadProfileModel* model, m_sourceModels) {
0146         int rowCount = model->rowCount(index.parent());
0147         if (index.row() - rowOffset < rowCount) {
0148             return model->uploadItem(model->index(index.row() - rowOffset, index.column()));
0149         }
0150         rowOffset += rowCount;
0151     }
0152     return nullptr;
0153 }
0154 
0155 UploadProfileItem* AllProfilesModel::uploadItem(int row, int column) const
0156 {
0157     return uploadItem(index(row, column));
0158 }
0159 
0160 // kate: space-indent on; indent-width 4; tab-width 4; replace-tabs on