Project

General

Profile

Drag and Drop from WTableView1 to WTableView2 ยป Test_Harness.cpp

Feeble attempt at between table drag and drop - David Betz, 08/28/2013 06:17 AM

 
/*
* Test_Harness.cpp
*
* Created on: Aug 21, 2013
* Author: dbetz
*/

#include <Wt/WApplication>
#include <Wt/WServer>
#include <Wt/WContainerWidget>
#include <Wt/WHBoxLayout>
#include <Wt/WTableView>
#include <Wt/WTextArea>
#include <Wt/WGroupBox>

#include <Wt/WAbstractTableModel>

class TableView1 : public Wt::WTableView {
};

class TableView2 : public Wt::WTableView {
};

class VirtualModel : public Wt::WAbstractTableModel
{
public:
VirtualModel(int rows, int columns, Wt::WObject *parent = 0)
: Wt::WAbstractTableModel(parent),
rows_(rows),
columns_(columns)
{ }

~VirtualModel(){ }

virtual int rowCount(const Wt::WModelIndex& parent = Wt::WModelIndex()) const
{
if (!parent.isValid())
return rows_;
else
return 0;
}

virtual int columnCount(const Wt::WModelIndex& parent = Wt::WModelIndex()) const
{
if (!parent.isValid())
return columns_;
else
return 0;
}

virtual boost::any data(const Wt::WModelIndex& index, int role = Wt::DisplayRole) const
{
switch (role) {
case Wt::DisplayRole:
if (index.column() == 0)
return Wt::WString("Row {1}").arg(index.row());
else
return Wt::WString("Item row {1}, col {2}")
.arg(index.row()).arg(index.column());
default:
return boost::any();
}
}

virtual boost::any headerData(int section,
Wt::Orientation orientation = Wt::Horizontal,
int role = Wt::DisplayRole) const
{
if (orientation == Wt::Horizontal) {
switch (role) {
case Wt::DisplayRole:
return Wt::WString("Column {1}").arg(section);
default:
return boost::any();
}
} else
return boost::any();
}

private:
int rows_, columns_;
};

class Test : Wt::WContainerWidget {
public:
Test(WContainerWidget *parent)
: WContainerWidget(parent){
Wt::WHBoxLayout *hBoxLayout;
Wt::WGroupBox *leftGroupBox, *rightGroupBox;
TableView1 *tableView1;
TableView2 *tableView2;
Wt::WTextArea *textArea;
VirtualModel *virtualModel1, *virtualModel2;

this->setLayout(hBoxLayout=new Wt::WHBoxLayout);

hBoxLayout->addWidget(leftGroupBox=new Wt::WGroupBox("Left Group Box"),1,0);
leftGroupBox->setWidth(Wt::WLength(34,Wt::WLength::Percentage));
leftGroupBox->addWidget(tableView1=new TableView1);
tableView1->resize(Wt::WLength::Auto, 200);
tableView1->setModel(virtualModel1=new VirtualModel(100,10));
tableView1->setMaximumSize(Wt::WLength::Auto, 200);
tableView1->setSelectable(true);
tableView1->setSelectionMode(Wt::SingleSelection);
tableView1->setDragEnabled(true);

hBoxLayout->addWidget(textArea=new Wt::WTextArea,1,0);
textArea->setWidth(Wt::WLength(32,Wt::WLength::Percentage));
textArea->setColumns(40);
textArea->setRows(10);

hBoxLayout->addWidget(rightGroupBox=new Wt::WGroupBox("Right Group Box"),1,0);
rightGroupBox->setWidth(Wt::WLength(34,Wt::WLength::Percentage));
rightGroupBox->addWidget(tableView2=new TableView2);
tableView2->resize(Wt::WLength::Auto, 200);
tableView2->setModel(virtualModel2=new VirtualModel(1,10));
tableView2->setMaximumSize(Wt::WLength::Auto, 200);
tableView2->setDropsEnabled(true);
tableView2->acceptDrops(virtualModel1->mimeType());
}
};

Wt::WApplication *createApplication(const Wt::WEnvironment& env)
{

Wt::WApplication *app = new Wt::WApplication(env);

app->setTitle("Test Harness");

app->messageResourceBundle().use(app->appRoot() + "strings");
app->messageResourceBundle().use(app->appRoot() + "templates");

new Test(app->root());

return app;
}


int main(int argc, char **argv)
{
try {
Wt::WServer server(argv[0]);

server.setServerConfiguration(argc, argv, WTHTTP_CONFIGURATION);
server.addEntryPoint(Wt::Application, createApplication);

if (server.start()) {
Wt::WServer::waitForShutdown();
server.stop();
}
} catch (Wt::WServer::Exception& e) {
std::cerr << e.what() << std::endl;
} catch (std::exception &e) {
std::cerr << "exception: " << e.what() << std::endl;
}
}
    (1-1/1)