Skip to content

Commit

Permalink
refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
haraki committed Jun 4, 2019
1 parent d22f128 commit fc91072
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 70 deletions.
166 changes: 106 additions & 60 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ MainWindow::MainWindow(QWidget *parent/* = Q_NULLPTR*/)
DoubleFolderPanel* doubleFolderPanel = new DoubleFolderPanel(ui->mainWidget);

connect(m_file,
SIGNAL(outputConsole(const QString)),
SIGNAL(outputConsole(const QString&)),
this,
SLOT(onOutputConsole(const QString)));
SLOT(onOutputConsole(const QString&)));
connect(this,
SIGNAL(outputConsole(const QString&)),
this,
SLOT(onOutputConsole(const QString&)));

connect(doubleFolderPanel,
SIGNAL(statusChanged(const QString&)),
Expand Down Expand Up @@ -175,91 +179,77 @@ void MainWindow::closeEvent(QCloseEvent* event)
QMainWindow::closeEvent(event);
}

void MainWindow::onOpenFile(ViewerType viewerType)
void MainWindow::emitOutputConsole(const QString& consoleString)
{
qDebug() << "MainWindow::onOpenFile()";

DoubleFolderPanel* doubleFolderPanel = ui->mainWidget->findChild<DoubleFolderPanel*>("DoubleFolderPanel");
if(doubleFolderPanel == Q_NULLPTR)
{
return;
}

FolderForm* activeFolderForm = doubleFolderPanel->getActiveFolderForm();
if(activeFolderForm == Q_NULLPTR)
{
return;
}

QString path = activeFolderForm->getCurrentFileInfo().absoluteFilePath();

onOpenFile(path, viewerType);
emit outputConsole(consoleString);
}

void MainWindow::onOpenFile(const QString& path, ViewerType viewerType/* = ViewerType::Auto*/)
int MainWindow::openFile(const QString& path, ViewerType viewerType/* = ViewerType::Auto*/)
{
DoubleFolderPanel* doubleFolderPanel = ui->mainWidget->findChild<DoubleFolderPanel*>("DoubleFolderPanel");
if(doubleFolderPanel == Q_NULLPTR)
{
return;
return -1;
}

if(QFileInfo(path).isDir())
{
// ディレクトリ移動
if(viewerType == ViewerType::Auto)
{
FolderForm* activeFolderForm = doubleFolderPanel->getActiveFolderForm();
if(activeFolderForm == Q_NULLPTR)
{
return;
return -1;
}

int ret = activeFolderForm->setPath(path);
if(ret < 0)
{
onOutputConsole(tr("Folder can not be opened.\n"));
emitOutputConsole(tr("Folder can not be opened : %1\n").arg(path));
}
}
else
{
onOutputConsole(tr("Folder can not be opened with the viewer.\n"));
emitOutputConsole(tr("Folder can not be opened with the viewer : %1\n").arg(path));
}

return;
}

ViewerBase* viewer = m_viewerDispatcher->dispatcher(path, viewerType);
if(viewer == Q_NULLPTR)
else
{
return;
}
// ファイルを Viewer でオープン
ViewerBase* viewer = m_viewerDispatcher->dispatcher(path, viewerType);
if(viewer == Q_NULLPTR)
{
return -1;
}

connect(viewer,
SIGNAL(closeViewer(const QString)),
this,
SLOT(onCloseViewer(const QString)));
connect(viewer,
SIGNAL(closeViewer(const QString)),
this,
SLOT(onCloseViewer(const QString)));

// 余計な操作ができないよう、ビュアー時はメニューは無効化
ui->menuBar->setEnabled(false);
// 余計な操作ができないよう、ビュアー時はメニューは無効化
ui->menuBar->setEnabled(false);

ui->mainWidget->layout()->addWidget(viewer);
ui->mainWidget->installEventFilter(viewer);
ui->mainWidget->layout()->addWidget(viewer);
ui->mainWidget->installEventFilter(viewer);

doubleFolderPanel->setVisible(false);
doubleFolderPanel->setVisible(false);

viewer->start(this);
viewer->start(this);
}

return 0;
}

void MainWindow::onCloseViewer(const QString& viewerObjectName)
int MainWindow::closeViewer(const QString& viewerObjectName)
{
qDebug() << "MainWindow::onCloseViewer()";

ui->menuBar->setEnabled(true);

QWidget* viewer = ui->mainWidget->findChild<QWidget*>(viewerObjectName);
if(viewer == Q_NULLPTR)
{
return;
return -1;
}

viewer->setVisible(false);
Expand All @@ -269,30 +259,32 @@ void MainWindow::onCloseViewer(const QString& viewerObjectName)
DoubleFolderPanel* doubleFolderPanel = ui->mainWidget->findChild<DoubleFolderPanel*>("DoubleFolderPanel");
if(doubleFolderPanel == Q_NULLPTR)
{
return;
return -1;
}

if(!doubleFolderPanel->isVisible())
{
doubleFolderPanel->setVisible(true);
}

return 0;
}

void MainWindow::onOpenWithApp(const QString& path)
int MainWindow::openWithApp(const QString& path)
{
qDebug() << "MainWindow::onOpenWithApp()";

if(!QDesktopServices::openUrl(QUrl("file:///" + path)))
{
qDebug() << "open url error:" << path;
onOutputConsole(tr("Open failed : ") + path);
emitOutputConsole(tr("Open failed : %1\n").arg(path));

return -1;
}

return 0;
}

void MainWindow::onOpenWithTextEditor(const QString& dirPath, const QStringList& filePaths)
int MainWindow::openWithTextEditor(const QString& dirPath, const QStringList& filePaths)
{
qDebug() << "MainWindow::onOpenWithTextEditor()";

QString appPath = Settings::getInstance()->getTextEditorPath();
QString args = Settings::getInstance()->getTextEditorArgs();

Expand All @@ -306,7 +298,7 @@ void MainWindow::onOpenWithTextEditor(const QString& dirPath, const QStringList&
launchPreferencesDialog(PreferencesDialogTabPage::ExternalApp);
}

return;
return 0;
}

#ifdef Q_OS_MAC
Expand All @@ -321,14 +313,61 @@ void MainWindow::onOpenWithTextEditor(const QString& dirPath, const QStringList&

qDebug() << "dirPath : " << dirPath << ", filePaths : " << filePaths << ", command : " << command;

launchExternalApp(command, dirPath);
if(!launchExternalApp(command, dirPath))
{
return -1;
}

return 0;
}

void MainWindow::onStatusChanged(const QString& statusString)
void MainWindow::onOpenFile(ViewerType viewerType)
{
qDebug() << "MainWindow::onStatusChanged : " << statusString;
qDebug() << "MainWindow::onOpenFile()";

statusBar()->showMessage(statusString);
DoubleFolderPanel* doubleFolderPanel = ui->mainWidget->findChild<DoubleFolderPanel*>("DoubleFolderPanel");
if(doubleFolderPanel == Q_NULLPTR)
{
return;
}

FolderForm* activeFolderForm = doubleFolderPanel->getActiveFolderForm();
if(activeFolderForm == Q_NULLPTR)
{
return;
}

QString path = activeFolderForm->getCurrentFileInfo().absoluteFilePath();

openFile(path, viewerType);
}

void MainWindow::onOpenFile(const QString& path, ViewerType viewerType/* = ViewerType::Auto*/)
{
qDebug() << "MainWindow::onOpenFile()";

openFile(path, viewerType);
}

void MainWindow::onCloseViewer(const QString& viewerObjectName)
{
qDebug() << "MainWindow::onCloseViewer()";

closeViewer(viewerObjectName);
}

void MainWindow::onOpenWithApp(const QString& path)
{
qDebug() << "MainWindow::onOpenWithApp()";

openWithApp(path);
}

void MainWindow::onOpenWithTextEditor(const QString& dirPath, const QStringList& filePaths)
{
qDebug() << "MainWindow::onOpenWithTextEditor()";

openWithTextEditor(dirPath, filePaths);
}

void MainWindow::onCreateNewFileFinished(const QString& filePath)
Expand All @@ -338,7 +377,14 @@ void MainWindow::onCreateNewFileFinished(const QString& filePath)
QStringList filePaths;
filePaths.push_back(filePath);

onOpenWithTextEditor(QFileInfo(filePath).absolutePath(), filePaths);
openWithTextEditor(QFileInfo(filePath).absolutePath(), filePaths);
}

void MainWindow::onStatusChanged(const QString& statusString)
{
qDebug() << "MainWindow::onStatusChanged : " << statusString;

statusBar()->showMessage(statusString);
}

void MainWindow::onOutputConsole(const QString& consoleString)
Expand Down
30 changes: 20 additions & 10 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ class MainWindow : public QMainWindow
explicit MainWindow(QWidget *parent = Q_NULLPTR);
~MainWindow() Q_DECL_OVERRIDE;

public Q_SLOTS:
void onOpenFile(ViewerType viewerType);
void onOpenFile(const QString& path, ViewerType viewerType = ViewerType::Auto);
void onCloseViewer(const QString& viewerName);
void onOpenWithApp(const QString& path);
void onOpenWithTextEditor(const QString& dirPath, const QStringList& filePaths);
void onStatusChanged(const QString& statusString);
void onCreateNewFileFinished(const QString& filePath);
void onOutputConsole(const QString& consoleString);

private:
void closeEvent(QCloseEvent* event) Q_DECL_OVERRIDE;

Expand All @@ -52,7 +42,27 @@ public Q_SLOTS:

void about();

Q_SIGNALS:
void outputConsole(const QString& consoleString);

private:
int openFile(const QString& path, ViewerType viewerType = ViewerType::Auto);
int closeViewer(const QString& viewerObjectName);
int openWithApp(const QString& path);
int openWithTextEditor(const QString& dirPath, const QStringList& filePaths);

void emitOutputConsole(const QString& consoleString);

private Q_SLOTS:
void onOpenFile(ViewerType viewerType);
void onOpenFile(const QString& path, ViewerType viewerType = ViewerType::Auto);
void onCloseViewer(const QString& viewerName);
void onOpenWithApp(const QString& path);
void onOpenWithTextEditor(const QString& dirPath, const QStringList& filePaths);
void onCreateNewFileFinished(const QString& filePath);
void onStatusChanged(const QString& statusString);
void onOutputConsole(const QString& consoleString);

void on_actionOpen_triggered();
void on_actionOpenWithTextViewer_triggered();
void on_actionOpenWithHexViewer_triggered();
Expand Down

0 comments on commit fc91072

Please sign in to comment.