📄 sfsmainwindow.cpp
字号:
#include "sfsmainwindow.h"#include <QtGui/QtGui>sfsMainWindow::sfsMainWindow(){ createActions(); createMenus(); createCentralWidget(); resize(240,320); setWindowTitle("User Manager"); reload();}void sfsMainWindow::createActions(){ uploadFileAction = new QAction(tr("Upload"),this); downloadFileAction = new QAction(tr("Download"),this); reloadAction = new QAction(tr("Reload"),this); deleteAction = new QAction(tr("Delete"),this); renameAction = new QAction(tr("Rename"),this); exitAction = new QAction(tr("Exit"),this); aboutAction = new QAction(tr("About"),this); connect(downloadFileAction,SIGNAL(triggered()),this,SLOT(downloadFile())); connect(uploadFileAction,SIGNAL(triggered()),this,SLOT(uploadFile())); connect(reloadAction,SIGNAL(triggered()),this,SLOT(reload())); connect(deleteAction,SIGNAL(triggered()),this,SLOT(deleteFile())); connect(renameAction,SIGNAL(triggered()),this,SLOT(rename())); connect(exitAction,SIGNAL(triggered()), qApp, SLOT(quit()));}void sfsMainWindow::createMenus(){ fileMenu = menuBar()->addMenu(tr("File")); fileMenu->addAction(uploadFileAction); fileMenu->addAction(downloadFileAction); fileMenu->addAction(reloadAction); fileMenu->addAction(deleteAction); fileMenu->addAction(renameAction); fileMenu->addAction(exitAction); helpMenu = menuBar()->addMenu(tr("Help")); helpMenu->addAction(aboutAction);}void sfsMainWindow::createCentralWidget(){ tableWidget = new QTableWidget(this); connect(tableWidget,SIGNAL(itemClicked(QTableWidgetItem *)),this,SLOT(enableAction(QTableWidgetItem *))); setCentralWidget(tableWidget);}void sfsMainWindow::uploadFile(){ QString upfilepath; QString upfilename; int upStat=1; upfilepath = QFileDialog::getOpenFileName(this,tr("Select File"), "./", tr("All Files (*.*)")); QStringList path = upfilepath.split("/"); upfilename = path[path.size()-1]; if (upfilename.size()>19) { QMessageBox::warning(this, tr("Error!"), tr("File name is too long!"), QMessageBox::Cancel, QMessageBox::Ok); } else { for(int i=0;i<tableWidget->rowCount();i++) { if (upfilename == tableWidget->item(i,0)->text()) { upStat = QMessageBox::warning(this, tr("File already exists"), tr("Would you like to replace it?"), QMessageBox::Cancel, QMessageBox::Ok); } } if (upStat != QMessageBox::Cancel && upfilepath != 0) { upfile(qPrintable(upfilename),qPrintable(upfilepath),upStat); reload(); } }}void sfsMainWindow::downloadFile(){ QString downfilepath; QString downfilename ; downfilename =tableWidget->item(tableWidget->currentRow(),0)->text(); downfilepath = QFileDialog::getExistingDirectory(this, tr("Open Directory"), "./",QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); if(downfilepath != 0) downfile(qPrintable(downfilename),qPrintable(downfilepath));}void sfsMainWindow::reload(){ listReload(); disableAction(); QFile file("liststring.dat"); file.open((QIODevice::ReadOnly)); QTextStream in(&file); QString liststring = in.readAll(); QStringList stringlist = liststring.split("|"); int listNum = 0; listNum = stringlist.size(); tableWidget->setRowCount(listNum/3); tableWidget->setColumnCount(3); //初始化水平表头条目名及宽度 QStringList headerLabel; headerLabel<<"FileName"<<"Size"<<"Uptime"; tableWidget->setHorizontalHeaderLabels(headerLabel); tableWidget->setColumnWidth(0,90); tableWidget->setColumnWidth(1,55); tableWidget->setColumnWidth(2,75); int x=0; for(int i=0;i<listNum/3;i++) { for(int j=0;j<3;j++) { QTableWidgetItem *items = new QTableWidgetItem; items->setText(stringlist[x++]); tableWidget->setItem(i,j,items); } } file.remove();}void sfsMainWindow::deleteFile(){ QString deletefilename ; deletefilename =tableWidget->item(tableWidget->currentRow(),0)->text(); deletefile(qPrintable(deletefilename)); reload();}void sfsMainWindow::rename(){ QString oldname; QString newname; bool ok; oldname = tableWidget->item(tableWidget->currentRow(),0)->text(); if (oldname != 0) { newname = QInputDialog::getText(this, tr("Rename File"), tr("Enter the new name:"), QLineEdit::Normal, oldname, &ok); } if (ok) renamefile(qPrintable(newname),qPrintable(oldname)); reload();}void sfsMainWindow::enableAction(QTableWidgetItem *item){ Q_UNUSED(item); downloadFileAction->setEnabled(true); deleteAction->setEnabled(true); renameAction->setEnabled(true);}void sfsMainWindow::disableAction(){ downloadFileAction->setEnabled(false); deleteAction->setEnabled(false); renameAction->setEnabled(false);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -