Browse Source

Show image size

Kandrashin Denis 12 years ago
parent
commit
622038f8ad
3 changed files with 62 additions and 17 deletions
  1. 8 8
      source/fb2head.hpp
  2. 43 7
      source/fb2temp.cpp
  3. 11 2
      source/fb2temp.hpp

+ 8 - 8
source/fb2head.hpp

@@ -137,14 +137,14 @@ public:
     void remove(const QModelIndex &index);
 
 public:
-    virtual Qt::ItemFlags flags(const QModelIndex &index) const;
-    virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
-    virtual QModelIndex parent(const QModelIndex &child) const;
-    virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
-    virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
-    virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
-    virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;
-    virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
+    Qt::ItemFlags flags(const QModelIndex &index) const;
+    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
+    QModelIndex parent(const QModelIndex &child) const;
+    int rowCount(const QModelIndex &parent = QModelIndex()) const;
+    int columnCount(const QModelIndex &parent = QModelIndex()) const;
+    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
+    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
+    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
 
 private:
     QWebView & m_view;

+ 43 - 7
source/fb2temp.cpp

@@ -17,6 +17,7 @@
 FbTemporaryFile::FbTemporaryFile(const QString &name)
     : QTemporaryFile()
     , m_name(name)
+    , m_size(0)
 {
 }
 
@@ -24,10 +25,9 @@ qint64 FbTemporaryFile::write(const QByteArray &data)
 {
     open();
     if (m_hash.isEmpty()) m_hash = md5(data);
-    qint64 size = QTemporaryFile::write(data);
+    m_size = QTemporaryFile::write(data);
     close();
-
-    return size;
+    return m_size;
 }
 
 QString FbTemporaryFile::md5(const QByteArray &data)
@@ -218,6 +218,14 @@ QString FbNetworkAccessManager::name(int index) const
     return QString();
 }
 
+int FbNetworkAccessManager::size(int index) const
+{
+    if (0 <= index && index < count()) {
+        return m_files[index]->size();
+    }
+    return 0;
+}
+
 QByteArray FbNetworkAccessManager::data(int index) const
 {
     if (0 <= index && index < count()) {
@@ -241,17 +249,45 @@ FbListModel::FbListModel(FbNetworkAccessManager &files, QObject *parent)
 {
 }
 
+int FbListModel::columnCount(const QModelIndex &parent) const
+{
+    Q_UNUSED(parent);
+    return 2;
+}
+
 int FbListModel::rowCount(const QModelIndex &parent) const
 {
     if (parent.isValid()) return 0;
     return m_files.count();
 }
 
+QVariant FbListModel::headerData(int section, Qt::Orientation orientation, int role) const
+{
+    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
+        switch (section) {
+            case 0: return tr("File name");
+            case 1: return tr("Size");
+        }
+    }
+    return QVariant();
+}
+
 QVariant FbListModel::data(const QModelIndex &index, int role) const
 {
     if (index.isValid()) {
         switch (role) {
-            case Qt::DisplayRole: return m_files.name(index.row());
+            case Qt::DisplayRole: {
+                switch (index.column()) {
+                    case 0: return m_files.name(index.row());
+                    case 1: return m_files.size(index.row());
+                }
+            } break;
+            case Qt::TextAlignmentRole: {
+                switch (index.column()) {
+                    case 0: return Qt::AlignLeft;
+                    case 1: return Qt::AlignRight;
+                }
+            }
         }
     }
     return QVariant();
@@ -265,7 +301,7 @@ QVariant FbListModel::data(const QModelIndex &index, int role) const
 #include <QScrollArea>
 
 FbListView::FbListView(FbNetworkAccessManager &files, QWidget *parent)
-    : QListView(parent)
+    : QTreeView(parent)
     , m_files(files)
 {
     m_label = new QLabel(this);
@@ -274,7 +310,7 @@ FbListView::FbListView(FbNetworkAccessManager &files, QWidget *parent)
 
 void FbListView::currentChanged(const QModelIndex &current, const QModelIndex &previous)
 {
-    QListView::currentChanged(current, previous);
+    QTreeView::currentChanged(current, previous);
 
     int row = current.row();
     if (0 <= row && row < model()->temp().count()) {
@@ -287,7 +323,7 @@ void FbListView::currentChanged(const QModelIndex &current, const QModelIndex &p
 
 FbListModel * FbListView::model() const
 {
-    return qobject_cast<FbListModel*>(QListView::model());
+    return qobject_cast<FbListModel*>(QTreeView::model());
 }
 
 //---------------------------------------------------------------------------

+ 11 - 2
source/fb2temp.hpp

@@ -9,7 +9,7 @@
 #include <QString>
 #include <QTemporaryFile>
 #include <QToolBar>
-#include <QListView>
+#include <QTreeView>
 
 class FbTextEdit;
 
@@ -24,10 +24,12 @@ public:
     void setHash(const QString &hash) { m_hash = hash; }
     const QString & hash() const { return m_hash; }
     const QString & name() const { return m_name; }
+    qint64 size() const { return m_size; }
     QByteArray data();
 private:
     const QString m_name;
     QString m_hash;
+    qint64 m_size;
 };
 
 class FbTemporaryList : public QList<FbTemporaryFile*>
@@ -96,6 +98,7 @@ public:
     int count() const { return m_files.count(); }
     QByteArray data(int index) const;
     QString name(int index) const;
+    int size(int index) const;
 
 protected:
     virtual QNetworkReply *createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData = 0);
@@ -114,15 +117,21 @@ class FbListModel : public QAbstractListModel
 
 public:
     explicit FbListModel(FbNetworkAccessManager &files, QObject *parent = 0);
+
+public:
     int rowCount(const QModelIndex &parent = QModelIndex()) const;
+    int columnCount(const QModelIndex &parent = QModelIndex()) const;
     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
+    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
+
+public:
     FbNetworkAccessManager &temp() { return m_files; }
 
 private:
     FbNetworkAccessManager &m_files;
 };
 
-class FbListView : public QListView
+class FbListView : public QTreeView
 {
     Q_OBJECT