Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

remove Q from item name, create helper function

git-svn-id: https://meyerhome.net/svn/src/trunk/source/ftpmodel@397 03e5c58e-5cf6-0310-a8fa-8d0411e8075b
  • Loading branch information...
commit ba862e5e0e7b13418441b33363fef778a6dca222 1 parent 83358ce
@icefox authored
Showing with 61 additions and 53 deletions.
  1. +20 −0 LICENSE
  2. +6 −0 README
  3. +29 −50 src/ftpmodel.cpp
  4. +6 −3 src/ftpmodel.h
View
20 LICENSE
@@ -0,0 +1,20 @@
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
6 README
@@ -0,0 +1,6 @@
+This is a ftpmodel written for Trolltech's Qt version 4. The model wraps QFtp in a model taking care of tree's and drag and drop and gives you easy ability to put the model inside a view.
+
+See this webpage for more information about Qt's model/view architecture: http://doc.trolltech.com/model-view-programming.html
+
+This code is under the BSD license.
+
View
79 src/ftpmodel.cpp
@@ -33,15 +33,15 @@
#include <QFileIconProvider>
#include <qmimedata.h>
-class QFtpItem {
+class FtpItem {
public:
- QFtpItem() : fetchedChildren(false), parent(0) {}
+ FtpItem() : fetchedChildren(false), parent(0) {}
inline bool isDir() const { return info.isDir(); }
QUrlInfo info;
bool fetchedChildren;
- QList<QFtpItem> children;
- QFtpItem *parent;
- inline bool operator <(const QFtpItem &item) const { return item.info.name() < info.name(); }
+ QList<FtpItem> children;
+ FtpItem *parent;
+ inline bool operator <(const FtpItem &item) const { return item.info.name() < info.name(); }
};
/*!
@@ -67,7 +67,7 @@ FtpModel::FtpModel(QObject *parent) : QAbstractItemModel(parent)
connect(&connection, SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int)));
connect(&connection, SIGNAL(commandFinished(int, bool)), this, SLOT(commandFinished(int, bool)));
connect(&connection, SIGNAL(commandStarted(int)), this, SLOT(commandStarted(int)));
- root = new QFtpItem();
+ root = new FtpItem();
iconProvider = new QFileIconProvider();
filters = QDir::Readable | QDir::Writable | QDir::Executable | QDir::NoDotAndDotDot;
}
@@ -88,9 +88,7 @@ int FtpModel::rowCount(const QModelIndex &parent) const
{
if (!connected())
return 0;
- const QFtpItem *item = parent.isValid() ?
- static_cast<const QFtpItem*>(parent.internalPointer()) : root;
- return item->children.count();
+ return ftpItem(parent)->children.count();
}
/*!
@@ -109,8 +107,7 @@ bool FtpModel::hasChildren(const QModelIndex &parent) const
{
if (!connected())
return false;
- const QFtpItem *item = parent.isValid() ?
- static_cast<const QFtpItem*>(parent.internalPointer()) : root;
+ const FtpItem *item = ftpItem(parent);
if (!parent.isValid())
return (item->children.count() > 0);
return item->isDir();
@@ -123,8 +120,7 @@ bool FtpModel::canFetchMore(const QModelIndex &parent) const
{
if (!connected())
return false;
- const QFtpItem *item = parent.isValid() ?
- static_cast<const QFtpItem*>(parent.internalPointer()) : root;
+ const FtpItem *item = ftpItem(parent);
return (!item->fetchedChildren);
}
@@ -136,8 +132,7 @@ void FtpModel::fetchMore(const QModelIndex &parent)
if (!connected())
return;
- QFtpItem *item = parent.isValid() ?
- static_cast<QFtpItem*>(parent.internalPointer()) : root;
+ FtpItem *item = index.isValid() ? static_cast<const FtpItem*>(index.internalPointer()) : root;
if (item->fetchedChildren)
return;
qDebug() << "fetch more" << (item == root) << parent.data().toString();
@@ -188,8 +183,7 @@ QString FtpModel::size(const QModelIndex &index) const
if (!connected())
return QString();
- const QFtpItem *item = index.isValid() ?
- static_cast<const QFtpItem*>(index.internalPointer()) : root;
+ const FtpItem *item = ftpItem(index);
if (item->isDir()) {
#ifdef Q_OS_MAC
@@ -217,8 +211,7 @@ QString FtpModel::type(const QModelIndex &index) const
if (!connected())
return QString();
- const QFtpItem *item = index.isValid() ?
- static_cast<const QFtpItem*>(index.internalPointer()) : root;
+ const FtpItem *item = ftpItem(index);
if (item->isDir())
return tr("Folder");
@@ -233,8 +226,7 @@ QString FtpModel::time(const QModelIndex &index) const
if (!connected())
return QString();
- const QFtpItem *item = index.isValid() ?
- static_cast<const QFtpItem*>(index.internalPointer()) : root;
+ const FtpItem *item = ftpItem(index);
#ifndef QT_NO_DATESTRING
return item->info.lastModified().toString(Qt::LocalDate);
@@ -254,8 +246,7 @@ bool FtpModel::setData(const QModelIndex &index, const QVariant &value, int role
if (role == Qt::EditRole && value.type() == QVariant::String){
// TODO finish
/*
- QFtpItem *item = index.isValid() ?
- static_cast<QFtpItem*>(index.internalPointer()) : root;
+ FtpItem *item = ftpItem(index);
QString oldName = fullPath(index);
QString newName = value.toString();
int id = connection->rename(oldName, newName);
@@ -283,8 +274,7 @@ Qt::ItemFlags FtpModel::flags(const QModelIndex &index) const
Qt::ItemFlags flags = QAbstractItemModel::flags(index);
flags |= Qt::ItemIsDragEnabled;
if (index.column() == 0) {
- QFtpItem *item = index.isValid() ?
- static_cast<QFtpItem*>(index.internalPointer()) : root;
+ const FtpItem *item = ftpItem(index);
if (item->info.isWritable())
flags |= Qt::ItemIsEditable;
if (item->isDir() && item->info.isWritable())
@@ -322,11 +312,11 @@ QModelIndex FtpModel::parent(const QModelIndex &index) const
if (!connected() || !index.isValid())
return QModelIndex();
- const QFtpItem *item = static_cast<const QFtpItem*>(index.internalPointer());
+ const FtpItem *item = static_cast<const FtpItem*>(index.internalPointer());
if (!item->parent || item->parent == (root))
return QModelIndex();
- const QFtpItem *grandparent = item->parent->parent;
+ const FtpItem *grandparent = item->parent->parent;
if (!grandparent || grandparent == (root))
return QModelIndex();
@@ -345,8 +335,7 @@ QModelIndex FtpModel::index(int row, int column, const QModelIndex &parent) cons
if (!connected() || !hasIndex(row, column, parent))
return QModelIndex();
- const QFtpItem *item = parent.isValid() ?
- static_cast<const QFtpItem*>(parent.internalPointer()) : root;
+ const FtpItem *item = ftpItem(parent);
return createIndex(row, column, (void*)(&(item->children[row])));
}
@@ -361,7 +350,7 @@ QModelIndex FtpModel::index(const QString &path, int column) const
return QModelIndex();
QStringList builtPath = (QDir(path).absolutePath()).split("/", QString::SkipEmptyParts);
- QFtpItem *item = root;
+ FtpItem *item = root;
int r = 0;
while (!builtPath.isEmpty()) {
@@ -388,9 +377,7 @@ QString FtpModel::fileName(const QModelIndex &index) const
{
if (!connected())
return QString();
- QFtpItem *item = index.isValid() ?
- static_cast<QFtpItem*>(index.internalPointer()) : root;
- return item->info.name();
+ return ftpItem(index)->info.name();
}
/*!
@@ -400,9 +387,7 @@ bool FtpModel::isDir(const QModelIndex &index) const
{
if (!connected())
return false;
- QFtpItem *item = index.isValid() ?
- static_cast<QFtpItem*>(index.internalPointer()) : root;
- return item->isDir();
+ return ftpItem(index)->isDir();
}
/*!
@@ -431,8 +416,7 @@ void FtpModel::refresh(const QModelIndex &parent)
if (!connected() || !isDir(parent))
return;
- QFtpItem *item = parent.isValid() ?
- static_cast<QFtpItem*>(parent.internalPointer()) : root;
+ FtpItem *item = index.isValid() ? static_cast<const FtpItem*>(index.internalPointer()) : root;
item->children.clear();
item->fetchedChildren = false;
fetchMore(parent);
@@ -445,8 +429,7 @@ QIcon FtpModel::fileIcon(const QModelIndex &index) const
{
if (!connected() || !index.isValid())
return QIcon();
- QFtpItem *item = index.isValid() ?
- static_cast<QFtpItem*>(index.internalPointer()) : root;
+ const FtpItem *item = ftpItem(index);
if (item->isDir())
return iconProvider->icon(QFileIconProvider::Folder);
@@ -461,8 +444,7 @@ QString FtpModel::filePath(const QModelIndex &index) const
if (!connected())
return QString();
- QFtpItem *item = index.isValid() ?
- static_cast<QFtpItem*>(index.internalPointer()) : root;
+ const FtpItem *item = ftpItem(index);
QStringList path;
while (item) {
path.append(item->info.name());
@@ -479,9 +461,7 @@ bool FtpModel::removeRows(int row, int count, const QModelIndex &parent)
if (!connected() || count < 1 || row < 0 || (row + count) > rowCount(parent))
return false;
- QFtpItem *item = parent.isValid() ?
- static_cast<QFtpItem*>(parent.internalPointer()) : root;
-
+ FtpItem *item = index.isValid() ? static_cast<const FtpItem*>(index.internalPointer()) : root;
beginRemoveRows(parent, row, row + count - 1);
// TODO ftp remove calls
@@ -556,7 +536,7 @@ bool FtpModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
/*!
Helper function to recursivly sort parent
*/
-void FtpModel::sort(QFtpItem *parent, Qt::SortOrder order) {
+void FtpModel::sort(FtpItem *parent, Qt::SortOrder order) {
qSort(parent->children);
int childCount = parent->children.count();
if (Qt::AscendingOrder != order) {
@@ -613,10 +593,9 @@ void FtpModel::gotNewListInfo(const QUrlInfo &info)
QModelIndex idx = index(listing.first());
beginInsertRows(idx, rowCount(idx), rowCount(idx));
- QFtpItem *parentItem = idx.isValid() ?
- static_cast<QFtpItem*>(idx.internalPointer()) : root;
+ FtpItem *parentItem = idx.isValid() ? static_cast<const FtpItem*>(idx.internalPointer()) : root;
- QFtpItem item;
+ FtpItem item;
item.parent = parentItem;
item.info = info;
item.fetchedChildren = !info.isDir();
@@ -628,7 +607,7 @@ void FtpModel::stateChanged(int state)
{
if (!connected() && root->fetchedChildren) {
delete root;
- root = new QFtpItem();
+ root = new FtpItem();
reset();
}
View
9 src/ftpmodel.h
@@ -36,7 +36,7 @@
#include <qhash.h>
#include <qpair.h>
-class QFtpItem;
+class FtpItem;
class QFileIconProvider;
class FtpModel : public QAbstractItemModel
@@ -107,8 +107,8 @@ private slots:
QUrl ftpUrl;
QDir::Filters filters;
QFileIconProvider *iconProvider;
- QFtpItem *root;
- void sort(QFtpItem *parent, Qt::SortOrder order);
+ FtpItem *root;
+ void sort(FtpItem *parent, Qt::SortOrder order);
QStringList listing;
QList<int> listingCommands;
@@ -118,6 +118,9 @@ private slots:
QHash<int, QFile*> copyCommands;
QHash<int, QFile*> moveCommands;
+ const FtpItem *ftpItem(const QModelIndex &index) const {
+ return index.isValid() ? static_cast<const FtpItem*>(index.internalPointer()) : root;
+ }
};
#endif // FTPMODEL_H
Please sign in to comment.
Something went wrong with that request. Please try again.