Permalink
Browse files

showing in qt model, but no child

  • Loading branch information...
nestal committed Apr 29, 2013
1 parent ee6408d commit 326ae20ca72d2380882e7237f5da323b75e121c9
View
@@ -20,12 +20,17 @@
#include "DriveModel.hh"
+#include "drive2/Resource.hh"
+
#include <QtCore/QDebug>
namespace gr {
-DriveModel::DriveModel( )
+using namespace v2;
+
+DriveModel::DriveModel( http::Agent *agent )
{
+ m_drv.Refresh( agent ) ;
}
Qt::ItemFlags DriveModel::flags( const QModelIndex& ) const
@@ -35,7 +40,17 @@ Qt::ItemFlags DriveModel::flags( const QModelIndex& ) const
QVariant DriveModel::data( const QModelIndex& index, int role ) const
{
- return role == Qt::DisplayRole ? QString("wow") : QVariant() ;
+ const Resource *res = Res(index) ;
+ if ( role == Qt::DisplayRole && res != 0 )
+ {
+ switch ( index.column() )
+ {
+ case 0: return QString::fromUtf8(res->Title().c_str()) ;
+ default: break ;
+ }
+ }
+
+ return QVariant() ;
}
QVariant DriveModel::headerData( int section, Qt::Orientation orientation, int role ) const
@@ -45,7 +60,7 @@ QVariant DriveModel::headerData( int section, Qt::Orientation orientation, int r
int DriveModel::rowCount( const QModelIndex& parent ) const
{
- return 10 ;
+ return Res(parent)->ChildCount() ;
}
int DriveModel::columnCount( const QModelIndex& parent ) const
@@ -55,12 +70,19 @@ int DriveModel::columnCount( const QModelIndex& parent ) const
bool DriveModel::hasChildren( const QModelIndex& parent ) const
{
- return parent.isValid() ? false : true ;
+ return Res(parent)->ChildCount() > 0 ;
}
QModelIndex DriveModel::index( int row, int column, const QModelIndex & parent ) const
{
- return parent.isValid() ? QModelIndex() : createIndex( row, column, 0 ) ;
+ return createIndex( row, column, const_cast<Resource*>(m_drv.Child(Res(parent), row)) ) ;
+}
+
+const Resource* DriveModel::Res( const QModelIndex& idx ) const
+{
+ return idx.isValid()
+ ? reinterpret_cast<const Resource*>(idx.internalPointer())
+ : m_drv.Root() ;
}
QModelIndex DriveModel::parent( const QModelIndex& idx ) const
View
@@ -22,12 +22,19 @@
#include <QtCore/QAbstractItemModel>
+#include "drive2/Drive.hh"
+
namespace gr {
+namespace http
+{
+ class Agent ;
+}
+
class DriveModel : public QAbstractItemModel
{
public :
- DriveModel( ) ;
+ DriveModel( http::Agent *agent ) ;
// QAbstractItemModel overrides
Qt::ItemFlags flags( const QModelIndex & index ) const ;
@@ -38,6 +45,12 @@ public :
bool hasChildren ( const QModelIndex& parent ) const ;
QModelIndex index( int row, int column, const QModelIndex& parent ) const ;
QModelIndex parent( const QModelIndex& idx ) const ;
+
+private :
+ const v2::Resource* Res( const QModelIndex& idx ) const ;
+
+private :
+ v2::Drive m_drv ;
} ;
} // end of namespace
View
@@ -24,7 +24,8 @@
namespace gr {
-MainWnd::MainWnd( )
+MainWnd::MainWnd( http::Agent *agent ) :
+ m_drive( agent )
{
m_ui.setupUi(this) ;
View
@@ -27,12 +27,17 @@
namespace gr {
+namespace http
+{
+ class Agent ;
+}
+
class MainWnd : public QMainWindow
{
Q_OBJECT
public :
- MainWnd( ) ;
+ MainWnd( http::Agent *agent ) ;
private :
Ui::MainWindow m_ui ;
View
@@ -78,11 +78,11 @@ int main( int argc, char **argv )
std::cout << feed.Content() << std::endl ;
}
*/
- Drive drive ;
- drive.Refresh( &agent ) ;
+// Drive drive ;
+// drive.Refresh( &agent ) ;
QApplication app( argc, argv ) ;
- MainWnd wnd ;
+ MainWnd wnd( &agent ) ;
wnd.show();
return app.exec() ;
@@ -27,6 +27,8 @@
#include <iostream>
#include <iterator>
+#include <cassert>
+
namespace gr { namespace v2 {
Drive::Drive( )
@@ -36,37 +38,64 @@ Drive::Drive( )
void Drive::Refresh( http::Agent *agent )
{
// get all folders first
-// Feed folders(
-// "https://www.googleapis.com/drive/v2/files?q=mimeType+%3d+%27application/vnd.google-apps.folder%27" ) ;
Feed folders( feeds::files ) ;
-// folders.Query( "mimeType", "application/vnd.google-apps.folder" ) ;
+ std::vector<std::pair<std::string, Resource*> > parent_child ;
while ( folders.Next( agent ) )
{
std::vector<Json> items = folders.Content()["items"].AsArray() ;
for ( std::vector<Json>::iterator i = items.begin() ; i != items.end() ; ++i )
{
- const Resource *r = Add( *i ) ;
+ Resource *r = NewResource( *i ) ;
+
+ Json parents ;
+ if ( i->Get( "parents", parents ) )
+ {
+ std::vector<std::string> pids ;
+ parents.Select<std::string>( "id", std::back_inserter(pids) ) ;
+
+ for ( std::vector<std::string>::iterator p = pids.begin() ; p != pids.end() ; ++p )
+ parent_child.push_back( std::make_pair( *p, r ) ) ;
+
+ // add to root node if no parent
+ if ( pids.empty() )
+ m_root.AddChild( r->ID() ) ;
+ }
}
}
+
+ for ( std::vector<std::pair<std::string, Resource*> >::iterator i = parent_child.begin() ;
+ i != parent_child.end() ; ++i )
+ {
+ Resource *parent = Find( i->first ) ;
+ if ( parent != 0 )
+ parent->AddChild( i->second->ID() ) ;
+ }
}
-const Resource* Drive::Add( const Json& item )
+Resource* Drive::FindRoot( http::Agent *agent )
+{
+ // get all folders first
+ Feed folders( feeds::files + "/root?fields=id" ) ;
+ folders.Next( agent ) ;
+
+ std::string id = folders.Content()["id"].Str() ;
+ std::cout << "root = " << id << std::endl ;
+
+ return Find( folders.Content()["id"].Str() ) ;
+}
+
+Resource* Drive::NewResource( const Json& item )
{
Resource *r = new Resource( item["id"].Str(), item["mimeType"].Str(), item["title"].Str() ) ;
// initialize parent IDs
- Json parents ;
- if ( item.Get( "parents", parents ) )
- {
- std::vector<std::string> parent_ids ;
- parents.Select<std::string>( "id", std::back_inserter(parent_ids) ) ;
-
- r->SetParent( parent_ids.begin(), parent_ids.end() ) ;
- std::cout << r->Title() << " " << r->ID() << " " << parent_ids.size() << std::endl ;
- }
+ std::cout << r->Title() << " " << r->ID() << std::endl ;
+
+ m_db.insert(r) ;
+ assert( Find(r->ID()) == r ) ;
- return *m_db.insert(r).first ;
+ return r ;
}
Resource* Drive::Find( const std::string& id )
@@ -75,5 +104,26 @@ Resource* Drive::Find( const std::string& id )
return i != m_db.get<details::ByID>().end() ? *i : 0 ;
}
+const Resource* Drive::Find( const std::string& id ) const
+{
+ details::ID::const_iterator i = m_db.get<details::ByID>().find(id) ;
+ return i != m_db.get<details::ByID>().end() ? *i : 0 ;
+}
+
+Resource* Drive::Root()
+{
+ return &m_root ;
+}
+
+const Resource* Drive::Root() const
+{
+ return &m_root ;
+}
+
+const Resource* Drive::Child( const Resource *parent, std::size_t idx ) const
+{
+ return Find( parent->At(idx) ) ;
+}
+
} } // end of namespace gr::v2
@@ -65,12 +65,21 @@ public :
void Refresh( http::Agent *agent ) ;
Resource* Find( const std::string& id ) ;
+ const Resource* Find( const std::string& id ) const ;
-private :
- const Resource* Add( const Json& item ) ;
+ Resource* Root() ;
+ const Resource* Root() const ;
+
+ const Resource* Child( const Resource *parent, std::size_t idx ) const ;
+private :
+ Resource* NewResource( const Json& item ) ;
+ Resource* FindRoot( http::Agent *agent ) ;
+
private :
details::DB m_db ;
+
+ Resource m_root ;
} ;
} } // end of namespace gr::v2
@@ -60,4 +60,19 @@ bool Resource::IsFolder() const
return m_mime == "application/vnd.google-apps.folder" ;
}
+void Resource::AddChild( const std::string& child )
+{
+ m_children.push_back( child ) ;
+}
+
+std::size_t Resource::ChildCount() const
+{
+ return m_children.size() ;
+}
+
+std::string Resource::At( std::size_t idx ) const
+{
+ return m_children.at(idx) ;
+}
+
} } // end of namespace gr::v2
@@ -31,24 +31,24 @@ public :
Resource() ;
Resource( const std::string& id, const std::string& mime, const std::string& title ) ;
- template <typename InputIt>
- void SetParent( InputIt first, InputIt last )
- {
- m_parent.assign( first, last ) ;
- }
-
std::string ID() const ;
std::string Mime() const ;
std::string Title() const ;
bool IsFolder() const ;
+ void AddChild( const std::string& child ) ;
+
+ std::size_t ChildCount() const ;
+
+ std::string At( std::size_t idx ) const ;
+
private :
std::string m_id ;
std::string m_mime ;
std::string m_title ;
- std::vector<std::string> m_parent ;
+ std::vector<std::string> m_children ;
} ;
} } // end of namespace gr::v2

0 comments on commit 326ae20

Please sign in to comment.