Guest User

Untitled

a guest
Oct 21st, 2013
97
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. QModelIndex num;
  5.  
  6. class MyItemDelegate: public QItemDelegate{
  7.     public:
  8.     MyItemDelegate(QObject * parent) : QItemDelegate(parent) {}
  9.         QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex & index) const{
  10.         QSize newSize = QItemDelegate::sizeHint(option, index);
  11.         newSize.setHeight(30);
  12.         return newSize;
  13.     }
  14. };
  15.  
  16. MainWindow::MainWindow(QWidget *parent) :
  17.     QMainWindow(parent),
  18.     ui(new Ui::MainWindow)
  19. {
  20.     QToolBar *toolBar = addToolBar(tr("Open"));
  21.     ui->setupUi(this);
  22.     combo = ui->comboBox;
  23.     listView = ui->listView;
  24.  
  25.     //combo
  26.     combo->setView(listView);
  27.     combo->setEditable(true);
  28.     //combo->setMinimumWidth(500);
  29.     //combo->setMinimumHeight(30);
  30.     connect(combo, SIGNAL(activated(int)), this, SLOT(openRssFeed()));
  31.  
  32.  
  33.  
  34.  
  35.  
  36.     QAction * start = new QAction(tr("Open RSS Feed"), this);
  37.     start->setIcon(style()->standardIcon(QStyle::SP_ArrowRight));
  38.     connect(start, SIGNAL(triggered()), this, SLOT(openRssFeed()));
  39.     toolBar->addAction(start);
  40.  
  41.     listView->setItemDelegate(new MyItemDelegate(this));
  42.     QPalette palette = listView->palette();
  43.     palette.setBrush(QPalette::Base,QColor(150,234,252));
  44.     listView->setPalette(palette);
  45.     connect(listView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(listViewDoubleClicked(QModelIndex)));
  46.     model = new QStandardItemModel(0, 1, this);
  47.     listView->setModel(model);
  48.  
  49.  
  50.  
  51.     progress = ui->progressBar;
  52.     progress->setMinimumWidth(650);
  53.     statusBar()->addPermanentWidget(progress);
  54.  
  55.     webView = ui->webView;
  56.     webView->load(QUrl(""));
  57.     connect(webView, SIGNAL(loadProgress(int)), progress, SLOT(setValue(int)));
  58.  
  59.     QSplitter *splitter = new QSplitter;
  60.     splitter->addWidget(listView);
  61.     splitter->addWidget(webView);
  62.     this->setCentralWidget(splitter);
  63.  
  64.     combo->addItem("http://news.rambler.ru/rss/scitech/");
  65.     combo->addItem("http://news.yandex.ru/hardware.rss");
  66.  
  67.     manager = new QNetworkAccessManager(this);
  68.     connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
  69.  
  70.     toolBar->addWidget(combo);
  71. }
  72.  
  73. MainWindow::~MainWindow()
  74. {
  75.     delete ui;
  76. }
  77.  
  78. void MainWindow::listViewDoubleClicked(const QModelIndex &index){
  79.     /* Load article in the web-view. */
  80.     QString strLink = index.data(Qt::UserRole).toString();
  81.     num = index;
  82.     webView->load(QUrl(strLink));
  83. }
  84.  
  85.  
  86. void MainWindow::openRssFeed(){
  87.     int i = combo->findText(combo->currentText());
  88.     if (i != -1){
  89.         combo->setCurrentIndex(i);
  90.     }
  91.     else{
  92.         combo->addItem(combo->currentText());
  93.         combo->setCurrentIndex(combo->count() - 1);
  94.     }
  95.     reply = manager->get(QNetworkRequest(QUrl(combo->currentText())));
  96.     connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
  97. }
  98.  
  99. void MainWindow::downloadProgress(qint64 bytes, qint64 bytesTotal){
  100.     if (bytesTotal == -1){
  101.         progress->setMinimum(0);
  102.         progress->setMaximum(0);
  103.     }
  104.     else{
  105.         progress->setMaximum(100);
  106.         int percent = bytes * 100 / bytesTotal;
  107.         progress->setValue(percent);
  108.     }
  109.  
  110. }
  111.  
  112. void MainWindow::replyFinished(QNetworkReply * netReply){
  113.     QString str (netReply->readAll());
  114.  
  115.     QVariant vt = netReply->attribute(QNetworkRequest::RedirectionTargetAttribute);
  116.  
  117.     delete reply;
  118.  
  119.     if (!vt.isNull()){
  120.         reply = manager->get(QNetworkRequest(vt.toUrl()));
  121.         connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
  122.     }
  123.     else{
  124.         QDomDocument doc;
  125.         QString error;
  126.         if (!doc.setContent(str, false, &error)){
  127.             webView->setHtml(QString("<h1>Error</h1>") + error);
  128.         }
  129.         else{
  130.             QDomElement docElem = doc.documentElement();
  131.             QDomNodeList nodeList = docElem.elementsByTagName("item");
  132.  
  133.             model->clear();
  134.             model->insertColumn(0);
  135.  
  136.             for (int i = 0; i < nodeList.length(); i++){
  137.                 QDomNode node = nodeList.item(i);
  138.                 QDomElement e = node.toElement();
  139.                 QString strTitle =  e.elementsByTagName("title").item(0).firstChild().nodeValue();
  140.                 QString strLink = e.elementsByTagName("link").item(0).firstChild().nodeValue();
  141.                 QString strDescription = e.elementsByTagName("description").item(0).firstChild().nodeValue();
  142.                 QString strToolTip = "<b>" + strTitle + "</b>" + "<br /><br />" + strDescription  +  "<br /><br />" + strLink;
  143.  
  144.                 model->insertRows(model->rowCount(), 1);
  145.                 QModelIndex index = model->index(model->rowCount() - 1, 0);
  146.                 model->setData(index, strTitle, Qt::DisplayRole);
  147.                 model->setData(index, style()->standardIcon(QStyle::SP_FileIcon), Qt::DecorationRole);
  148.                 model->setData(index, strToolTip, Qt::ToolTipRole);
  149.                 model->setData(index, strLink, Qt::UserRole);
  150.                 model->itemFromIndex(index)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  151.             }
  152.         }
  153.     }
  154. }
  155.  
  156.  
  157. void ListView::keyPressEvent(QKeyEvent * event){
  158.     if (event->key() == Qt::Key_Delete){
  159.         event->accept();
  160.         QModelIndexList l = selectedIndexes();
  161.         if (l.length() > 0){
  162.             model()->removeRow(l.at(0).row(), l.at(0).parent());
  163.         }
  164.     }
  165.     else{
  166.         QListView::keyPressEvent(event);
  167.     }
  168. }
  169.  
  170.  
  171.  
  172.  
  173. void MainWindow::on_pushButton_clicked()
  174. {
  175.     QString strLink = num.data(Qt::UserRole).toString();
  176.     webView->load(QUrl(strLink));
  177. }
RAW Paste Data