📄 mainwindow.cpp
字号:
/* * nzb * * Copyright (C) 2004-2006 Mattias Nordstrom <matta at ftlight net> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * * Authors: * Mattias Nordstrom <matta at ftlight net> * * $Id: mainwindow.cpp,v 1.18 2005/10/16 12:03:32 mnordstr Exp $ * This file provides the GUI. */#include <QtGui>#include "mainwindow.h"#include "downloader.h"MainWindow::MainWindow(){ shutting_down = false; split = new QSplitter(Qt::Vertical, this); l = new QTableWidget(split); l->setColumnCount(4); l->setHorizontalHeaderLabels((QStringList)"Subject" << "Progress" << "Parts" << "Size"); l->setSelectionMode(QAbstractItemView::NoSelection); l->setSelectionBehavior(QAbstractItemView::SelectRows); l->setShowGrid(false); l->verticalHeader()->hide(); d = new QTableWidget(split); d->setColumnCount(3); d->setHorizontalHeaderLabels((QStringList)"Thread" << "Status" << "Speed"); d->setSelectionMode(QAbstractItemView::NoSelection); d->setSelectionBehavior(QAbstractItemView::SelectRows); d->setShowGrid(false); d->verticalHeader()->hide(); setCentralWidget(split); createActions(); createMenus(); createToolBars(); createStatusBar(); readSettings(); d->setRowCount(config->value("server/connections").toInt() + 1); int i; for (i=0; i<config->value("server/connections").toInt(); i++) { downloaders.append(new Downloader(&nzblist, i, &file_lock, l, this)); d->setItem(i, 0, new QTableWidgetItem(QIcon(":/images/connect_no.png"), "Downloader #"+QString::number(i+1))); d->setItem(i, 2, new QTableWidgetItem("0 kB/s")); d->item(i, 2)->setTextAlignment(Qt::AlignRight); connect(this, SIGNAL(fullStop()), downloaders[i], SLOT(stop())); } decoders.append(new Decoder(&nzblist, i, &file_lock, this)); d->setItem(i, 0, new QTableWidgetItem(QIcon(":/images/decoder.png"), "Decoder #1")); connect(this, SIGNAL(processDecoder()), decoders[0], SLOT(processFiles())); connect(this, SIGNAL(fullStop()), decoders[0], SLOT(stop())); output = new Output(&nzblist, i+1, &file_lock, this); connect(this, SIGNAL(processOutput()), output, SLOT(process())); connect(this, SIGNAL(fullStop()), output, SLOT(stop())); int drows = d->rowCount(); for (int row=0; row<drows; row++) { d->resizeRowToContents(row); } setWindowTitle(tr("nzb"));}void MainWindow::closeEvent(QCloseEvent *event){ shutting_down = true; if (stopAct->isEnabled()) { stop(); } writeSettings(); event->accept();}void MainWindow::open(){ QString fileName = QFileDialog::getOpenFileName(this, "Open NZB File", NULL, "nzb Files (*.nzb)\nAll Files (*)"); if (!fileName.isEmpty()) loadFile(fileName);}void MainWindow::about(){ QMessageBox::about(this, tr("About nzb"), tr("nzb v%1\n\nnzb is a Usenet binary downloader.\n\nSee http://nzb.sf.net/").arg(NZB_VERSION));}void MainWindow::createActions(){ openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this); openAct->setShortcut(tr("Ctrl+O")); openAct->setStatusTip(tr("Open a file")); connect(openAct, SIGNAL(triggered()), this, SLOT(open())); exitAct = new QAction(tr("E&xit"), this); exitAct->setShortcut(tr("Ctrl+Q")); exitAct->setStatusTip(tr("Exit the application")); connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); sortAct = new QAction(tr("&Sort"), this); sortAct->setStatusTip(tr("Sort the nzb list")); connect(sortAct, SIGNAL(triggered()), this, SLOT(sortList())); clearAct = new QAction(tr("&Clear"), this); clearAct->setStatusTip(tr("Clear the nzb list")); connect(clearAct, SIGNAL(triggered()), this, SLOT(clearList())); aboutAct = new QAction(tr("&About"), this); aboutAct->setStatusTip(tr("Show the application's About box")); connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); optionsAct = new QAction(tr("&Options"), this); optionsAct->setStatusTip(tr("Show the options dialog")); connect(optionsAct, SIGNAL(triggered()), this, SLOT(options())); startAct = new QAction(QIcon(":/images/start.png"), tr("&Start"), this); startAct->setShortcut(tr("Ctrl+S")); startAct->setStatusTip(tr("Start downloader")); startAct->setEnabled(false); connect(startAct, SIGNAL(triggered()), this, SLOT(start())); stopAct = new QAction(QIcon(":/images/stop.png"), tr("S&top"), this); stopAct->setShortcut(tr("Ctrl+T")); stopAct->setStatusTip(tr("Stop downloader")); stopAct->setEnabled(false); connect(stopAct, SIGNAL(triggered()), this, SLOT(stop())); streamAct = new QAction(QIcon(":/images/stream.png"), tr("St&ream"), this); streamAct->setShortcut(tr("Ctrl+R")); streamAct->setStatusTip(tr("Start streaming")); streamAct->setEnabled(false); connect(streamAct, SIGNAL(triggered()), this, SLOT(stream()));}void MainWindow::createMenus(){ fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(openAct); fileMenu->addSeparator(); fileMenu->addAction(exitAct); menuBar()->addSeparator(); editMenu = menuBar()->addMenu(tr("&Edit")); editMenu->addAction(sortAct); editMenu->addAction(clearAct); menuBar()->addSeparator(); actionsMenu = menuBar()->addMenu(tr("&Actions")); actionsMenu->addAction(startAct); actionsMenu->addAction(stopAct); actionsMenu->addAction(streamAct); menuBar()->addSeparator(); toolsMenu = menuBar()->addMenu(tr("&Tools")); toolsMenu->addAction(optionsAct); menuBar()->addSeparator(); helpMenu = menuBar()->addMenu(tr("&Help")); helpMenu->addAction(aboutAct);}void MainWindow::createToolBars(){ fileToolBar = addToolBar(tr("File")); fileToolBar->addAction(openAct); actionToolBar = addToolBar(tr("Actions")); actionToolBar->addAction(startAct); actionToolBar->addAction(stopAct); actionToolBar->addAction(streamAct);}void MainWindow::createStatusBar(){ statusBar()->showMessage(tr("Ready")); totalSpeed = new QLabel("0 kB/s"); totalSize = new QLabel("0 MB"); totalProgress = new QProgressBar(); totalProgress->setRange(0, 1); totalProgress->setValue(0); statusBar()->addPermanentWidget(totalSpeed); statusBar()->addPermanentWidget(totalProgress); statusBar()->addPermanentWidget(totalSize);}void MainWindow::readSettings(){ if (QFile::exists("nzb.ini")) { config = new QSettings("nzb.ini", QSettings::IniFormat); } else { config = new QSettings("nzb", "nzb"); } QPoint pos = config->value("mainwindow/pos", QPoint(200, 200)).toPoint(); QSize size = config->value("mainwindow/size", QSize(400, 400)).toSize(); resize(size); move(pos); split->restoreState(config->value("mainwindow/split").toByteArray()); l->horizontalHeader()->resizeSection(0, config->value("mainwindow/listcol0width", 50).toInt()); l->horizontalHeader()->resizeSection(1, config->value("mainwindow/listcol1width", 50).toInt()); l->horizontalHeader()->resizeSection(2, config->value("mainwindow/listcol2width", 50).toInt()); l->horizontalHeader()->resizeSection(3, config->value("mainwindow/listcol3width", 50).toInt()); d->horizontalHeader()->resizeSection(0, config->value("mainwindow/dlistcol0width", 50).toInt()); d->horizontalHeader()->resizeSection(1, config->value("mainwindow/dlistcol1width", 50).toInt()); d->horizontalHeader()->resizeSection(2, config->value("mainwindow/dlistcol2width", 50).toInt()); // Fix for situation when the default value isn't honored. if (l->columnWidth(0) == 0) l->horizontalHeader()->resizeSection(0, 50); if (l->columnWidth(1) == 0) l->horizontalHeader()->resizeSection(1, 50); if (l->columnWidth(2) == 0) l->horizontalHeader()->resizeSection(2, 50); if (l->columnWidth(3) == 0) l->horizontalHeader()->resizeSection(3, 50); if (d->columnWidth(0) == 0) d->horizontalHeader()->resizeSection(0, 50); if (d->columnWidth(1) == 0) d->horizontalHeader()->resizeSection(1, 50); if (d->columnWidth(2) == 0) d->horizontalHeader()->resizeSection(2, 50);}void MainWindow::writeSettings(){ config->setValue("mainwindow/pos", pos()); config->setValue("mainwindow/size", size()); config->setValue("mainwindow/split", split->saveState()); config->setValue("mainwindow/listcol0width", l->columnWidth(0)); config->setValue("mainwindow/listcol1width", l->columnWidth(1)); config->setValue("mainwindow/listcol2width", l->columnWidth(2)); config->setValue("mainwindow/listcol3width", l->columnWidth(3)); config->setValue("mainwindow/dlistcol0width", d->columnWidth(0)); config->setValue("mainwindow/dlistcol1width", d->columnWidth(1)); config->setValue("mainwindow/dlistcol2width", d->columnWidth(2)); config->sync();}void MainWindow::populateList(){ int i; int row = l->rowCount(); l->setRowCount(nzblist.getList()->count()); for (i=row; i<l->rowCount(); i++) { QTableWidgetItem *newItem = new QTableWidgetItem(nzblist.getFile(i)->getSubject()); QTableWidgetItem *newItem_prog = new QTableWidgetItem("0"); QTableWidgetItem *newItem_tot = new QTableWidgetItem(QString::number(nzblist.getFile(i)->getSegments()->size())); QTableWidgetItem *newItem_size; int file_bytes = nzblist.getFile(i)->getBytes(); /*if (file_bytes > 999999999) { newItem_size = new QTableWidgetItem(QString::number((file_bytes / 1073741824))+" GB"); } else*/ if (file_bytes > 999999) { newItem_size = new QTableWidgetItem(QString::number((file_bytes / 1048576))+" MB"); } else if (file_bytes > 999) { newItem_size = new QTableWidgetItem(QString::number((file_bytes / 1024))+" kB"); } else { newItem_size = new QTableWidgetItem(QString::number(file_bytes)+" bytes"); } newItem->setCheckState(Qt::Checked); l->setItem(row, 0, newItem); l->setItem(row, 1, newItem_prog); l->setItem(row, 2, newItem_tot); l->setItem(row, 3, newItem_size); newItem_prog->setTextAlignment(Qt::AlignRight); newItem_tot->setTextAlignment(Qt::AlignRight); newItem_size->setTextAlignment(Qt::AlignRight); if (config->value("list/par_uncheck").toBool()) { QRegExp rx("*vol*.par2"); rx.setPatternSyntax(QRegExp::Wildcard); rx.setCaseSensitivity(Qt::CaseInsensitive); if (nzblist.getFile(i)->getSubject().contains(rx)) { newItem->setCheckState(Qt::Unchecked); } } l->resizeRowToContents(row); row++; } qint64 total_size = nzblist.totalSize(); if (total_size > 999999999) { totalSize->setText(QString::number((double)((double)(total_size / 1048576) / 1000), 'f', 2)+" GB"); } else if (total_size > 999999) { totalSize->setText(QString::number((total_size / 1048576))+" MB"); } else if (total_size > 999) { totalSize->setText(QString::number((total_size / 1024))+" kB"); } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -