Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- QModelIndex num;
- class MyItemDelegate: public QItemDelegate{
- public:
- MyItemDelegate(QObject * parent) : QItemDelegate(parent) {}
- QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex & index) const{
- QSize newSize = QItemDelegate::sizeHint(option, index);
- newSize.setHeight(30);
- return newSize;
- }
- };
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- QToolBar *toolBar = addToolBar(tr("Open"));
- ui->setupUi(this);
- combo = ui->comboBox;
- listView = ui->listView;
- //combo
- combo->setView(listView);
- combo->setEditable(true);
- //combo->setMinimumWidth(500);
- //combo->setMinimumHeight(30);
- connect(combo, SIGNAL(activated(int)), this, SLOT(openRssFeed()));
- QAction * start = new QAction(tr("Open RSS Feed"), this);
- start->setIcon(style()->standardIcon(QStyle::SP_ArrowRight));
- connect(start, SIGNAL(triggered()), this, SLOT(openRssFeed()));
- toolBar->addAction(start);
- listView->setItemDelegate(new MyItemDelegate(this));
- QPalette palette = listView->palette();
- palette.setBrush(QPalette::Base,QColor(150,234,252));
- listView->setPalette(palette);
- connect(listView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(listViewDoubleClicked(QModelIndex)));
- model = new QStandardItemModel(0, 1, this);
- listView->setModel(model);
- progress = ui->progressBar;
- progress->setMinimumWidth(650);
- statusBar()->addPermanentWidget(progress);
- webView = ui->webView;
- webView->load(QUrl(""));
- connect(webView, SIGNAL(loadProgress(int)), progress, SLOT(setValue(int)));
- QSplitter *splitter = new QSplitter;
- splitter->addWidget(listView);
- splitter->addWidget(webView);
- this->setCentralWidget(splitter);
- combo->addItem("http://news.rambler.ru/rss/scitech/");
- combo->addItem("http://news.yandex.ru/hardware.rss");
- manager = new QNetworkAccessManager(this);
- connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
- toolBar->addWidget(combo);
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
- void MainWindow::listViewDoubleClicked(const QModelIndex &index){
- /* Load article in the web-view. */
- QString strLink = index.data(Qt::UserRole).toString();
- num = index;
- webView->load(QUrl(strLink));
- }
- void MainWindow::openRssFeed(){
- int i = combo->findText(combo->currentText());
- if (i != -1){
- combo->setCurrentIndex(i);
- }
- else{
- combo->addItem(combo->currentText());
- combo->setCurrentIndex(combo->count() - 1);
- }
- reply = manager->get(QNetworkRequest(QUrl(combo->currentText())));
- connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
- }
- void MainWindow::downloadProgress(qint64 bytes, qint64 bytesTotal){
- if (bytesTotal == -1){
- progress->setMinimum(0);
- progress->setMaximum(0);
- }
- else{
- progress->setMaximum(100);
- int percent = bytes * 100 / bytesTotal;
- progress->setValue(percent);
- }
- }
- void MainWindow::replyFinished(QNetworkReply * netReply){
- QString str (netReply->readAll());
- QVariant vt = netReply->attribute(QNetworkRequest::RedirectionTargetAttribute);
- delete reply;
- if (!vt.isNull()){
- reply = manager->get(QNetworkRequest(vt.toUrl()));
- connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
- }
- else{
- QDomDocument doc;
- QString error;
- if (!doc.setContent(str, false, &error)){
- webView->setHtml(QString("<h1>Error</h1>") + error);
- }
- else{
- QDomElement docElem = doc.documentElement();
- QDomNodeList nodeList = docElem.elementsByTagName("item");
- model->clear();
- model->insertColumn(0);
- for (int i = 0; i < nodeList.length(); i++){
- QDomNode node = nodeList.item(i);
- QDomElement e = node.toElement();
- QString strTitle = e.elementsByTagName("title").item(0).firstChild().nodeValue();
- QString strLink = e.elementsByTagName("link").item(0).firstChild().nodeValue();
- QString strDescription = e.elementsByTagName("description").item(0).firstChild().nodeValue();
- QString strToolTip = "<b>" + strTitle + "</b>" + "<br /><br />" + strDescription + "<br /><br />" + strLink;
- model->insertRows(model->rowCount(), 1);
- QModelIndex index = model->index(model->rowCount() - 1, 0);
- model->setData(index, strTitle, Qt::DisplayRole);
- model->setData(index, style()->standardIcon(QStyle::SP_FileIcon), Qt::DecorationRole);
- model->setData(index, strToolTip, Qt::ToolTipRole);
- model->setData(index, strLink, Qt::UserRole);
- model->itemFromIndex(index)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
- }
- }
- }
- }
- void ListView::keyPressEvent(QKeyEvent * event){
- if (event->key() == Qt::Key_Delete){
- event->accept();
- QModelIndexList l = selectedIndexes();
- if (l.length() > 0){
- model()->removeRow(l.at(0).row(), l.at(0).parent());
- }
- }
- else{
- QListView::keyPressEvent(event);
- }
- }
- void MainWindow::on_pushButton_clicked()
- {
- QString strLink = num.data(Qt::UserRole).toString();
- webView->load(QUrl(strLink));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement