csvview.cpp Example File
webkit/webplugin/csvview.cpp
 
 
 #include <QtGui>
 #include <QtNetwork>
 #include "csvview.h"
 CSVView::CSVView(const QString &mimeType, QWidget *parent)
     : QTableView(parent)
 {
     this->mimeType = mimeType;
     setEditTriggers(NoEditTriggers);
     setSelectionBehavior(SelectRows);
     setSelectionMode(SingleSelection);
 }
 void CSVView::updateModel()
 {
     QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
     if (reply->error() != QNetworkReply::NoError)
         return;
     bool hasHeader = false;
     QString charset = "latin1";
     foreach (QString piece, mimeType.split(";")) {
         piece = piece.trimmed();
         if (piece.contains("=")) {
             int index = piece.indexOf("=");
             QString left = piece.left(index).trimmed();
             QString right = piece.mid(index + 1).trimmed();
             if (left == "header")
                 hasHeader = (right == "present");
             else if (left == "charset")
                 charset = right;
         }
     }
     QTextStream stream(reply);
     stream.setCodec(QTextCodec::codecForName(charset.toLatin1()));
     QStandardItemModel *model = new QStandardItemModel(this);
     QList<QStandardItem *> items;
     bool firstLine = hasHeader;
     bool wasQuote = false;
     bool wasCR = false;
     bool quoted = false;
     QString text;
     while (!stream.atEnd()) {
         QString ch = stream.read(1);
         if (wasQuote) {
             if (ch == "\"") {
                 if (quoted) {
                     text += ch;         
                     wasQuote = false;   
                 } else {
                     quoted = true;      
                     wasQuote = true;    
                 }
                 continue;               
             } else {
                 quoted = !quoted;       
                 wasQuote = false;       
             }                           
         } else if (wasCR) {
             wasCR = false;
             if (ch == "\n") {           
                 if (!text.isEmpty())
                     items.append(new QStandardItem(QString(text)));
                 addRow(firstLine, model, items);
                 items.clear();
                 text = "";
                 firstLine = false;
                 continue;               
             } else
                 text += "\r";           
         }                               
         
         
         if (ch == "\"")
             wasQuote = true;            
         else if (ch == ",") {
             if (quoted)
                 text += ch;
             else {
                 items.append(new QStandardItem(QString(text)));
                 text = "";
             }
         }
         else if (ch == "\r") {
             if (!quoted)
                 wasCR = true;
             else
                 text += ch;
         }
         else if (ch == "\n")
             text += ch;
         else
             text += ch;
     }
     if (items.count() > 0)
         addRow(firstLine, model, items);
     reply->close();
     setModel(model);
     connect(selectionModel(),
             SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
             this, SLOT(exportRow(const QModelIndex &)));
     resizeColumnsToContents();
     horizontalHeader()->setStretchLastSection(true);
 }
 void CSVView::addRow(bool firstLine, QStandardItemModel *model,
                      const QList<QStandardItem *> &items)
 {
     if (firstLine) {
         for (int j = 0; j < items.count(); ++j)
             model->setHorizontalHeaderItem(j, items[j]);
     } else
         model->appendRow(items);
 }
 void CSVView::exportRow(const QModelIndex ¤t)
 {
     QString name = model()->index(current.row(), 0).data().toString();
     QString address = model()->index(current.row(), 1).data().toString();
     QString quantity = model()->index(current.row(), 2).data().toString();
     emit rowSelected(name, address, quantity);
 }