#include <QPushButton> #include <QWidget> #include <QString> #include <QLabel> #include <QLineEdit> #include <QGridLayout> #include <QKeyEvent> #include <QStatusBar> #include <QComboBox> #include <QTextEdit> #include <QFile> #include <QMessageBox> #include "FtpWindow.h" #include "FtpBrowser.h" FtpWindow::FtpWindow(QWidget *parent):QMainWindow(parent){ editor=false; createLogin(); setCentralWidget(login); ftp=new FtpBrowser(statusBar()); connect(this, SIGNAL(sendLoginInfo(QString, QString, QString, QString)), ftp, SLOT(open(QString, QString, QString, QString))); connect(ftp, SIGNAL(connected()), this, SLOT(browse())); } void FtpWindow::createLogin(){ static const QString labels[INPUTS]={ tr("&Server"), tr("&Kasutaja"), tr("S&alasõna"), tr("Ka&taloog") }; login=new QWidget; QGridLayout *l = new QGridLayout; for(int i=0;i<INPUTS;i++){ inputs[i]=new QLineEdit(login); inputs[i]->setMinimumWidth(100); QLabel *label=new QLabel(labels[i], login); label->setBuddy(inputs[i]); l->addWidget(label, 1, i*2); l->addWidget(inputs[i], 1, i*2+1); } inputs[2]->setEchoMode(QLineEdit::Password); connBut=new QPushButton(tr("&Ühenda"), login); l->addWidget(connBut, 2, INPUTS); statusBar()->showMessage(tr("Sisesta serveri info")); login->setLayout(l); connect(connBut, SIGNAL(pressed()), this, SLOT(connectB())); } void FtpWindow::createBrowser(){ browser=new QWidget; QGridLayout *l=new QGridLayout; fileName=new QLineEdit(browser); list=new QComboBox(browser); edit=new QTextEdit(browser); QPushButton *open=new QPushButton(tr("&Ava"), browser), *save=new QPushButton(tr("&Salvesta"), browser), *del=new QPushButton(tr("&Kustuta"), browser); list->setMinimumWidth(150); l->addWidget(fileName, 1, 2); l->addWidget(list, 1, 3); l->addWidget(open, 1, 4); l->addWidget(save, 1, 5); l->addWidget(del, 1, 6); l->addWidget(edit, 2, 1, 4, 6); browser->setLayout(l); connect(ftp->getFtp(), SIGNAL(listInfo(const QUrlInfo &)),this, SLOT(addToList(const QUrlInfo &))); connect(open, SIGNAL(clicked()), this, SLOT(openB())); connect(save, SIGNAL(clicked()), this, SLOT(saveB())); connect(del, SIGNAL(clicked()), this, SLOT(delB())); connect(ftp, SIGNAL(downloaded(QString)), this, SLOT(open(QString))); connect(this, SIGNAL(save(QString, QString)), ftp, SLOT(upload(QString, QString))); connect(this, SIGNAL(download(QString)), ftp, SLOT(download(QString))); connect(this, SIGNAL(remove(QString)), ftp, SLOT(remove(QString))); connect(list, SIGNAL(currentIndexChanged(const QString &)), fileName, SLOT(setText(const QString &))); connect(ftp, SIGNAL(removeStatus(bool)), this, SLOT(removeStatus(bool))); } void FtpWindow::connectB(){ emit sendLoginInfo(inputs[0]->text(), inputs[1]->text(), inputs[2]->text(), inputs[3]->text()); } void FtpWindow::saveB(){ emit save(edit->toPlainText(), fileName->text()); emit list->clear(); } void FtpWindow::openB(){ emit download(fileName->text()); } void FtpWindow::delB(){ if (QMessageBox::question(this, tr("FTP notepad"), tr("Oled kindel et soovid faili kustutada?\n"), tr("&Jah"), tr("&Ei"),0,1)==0){ emit remove(fileName->text()); } } void FtpWindow::browse(){ editor=true; createBrowser(); setCentralWidget(browser); } void FtpWindow::open(QString filename){ this->file=new QFile(filename); file->open(QIODevice::ReadOnly); edit->setDocument(new QTextDocument(file->readAll())); statusBar()->showMessage(tr("Dokument avatud")); file->close(); } void FtpWindow::keyReleaseEvent(QKeyEvent *event){ const int k=event->key(); if (!editor &&(k==Qt::Key_Enter || k==Qt::Key_Return)){ emit connBut->click(); } else { event->ignore(); } } void FtpWindow::addToList(const QUrlInfo &urlInfo){ if (!urlInfo.isDir()){ list->addItem(urlInfo.name(), QVariant(new QUrlInfo(urlInfo))); } } void FtpWindow::removeStatus(bool state){ if (state){ emit list->clear(); emit ftp->getFtp()->list(); } }