Skip to content

Commit

Permalink
Try to fix opening containg folder (variar#112, variar#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
variar committed Sep 30, 2019
1 parent 0a9740c commit 373352c
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 24 deletions.
Binary file added src/app/images/icons8-sync-16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 4 additions & 10 deletions src/ui/src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#ifdef Q_OS_WIN
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif // _WIN32
#endif // Q_OS_WIN


#include <QAction>
Expand All @@ -58,18 +58,16 @@
#include <QClipboard>
#include <QMessageBox>
#include <QCloseEvent>
#include <QDragEnterEvent>
#include <QMimeData>
#include <QUrl>
#include <QWindow>
#include <QTemporaryFile>
#include <QDesktopServices>

#include "log.h"
#include "openfilehelper.h"

#include "mainwindow.h"

#include "sessioninfo.h"
#include "recentfiles.h"
#include "crawlerwidget.h"
#include "highlightersdialog.h"
Expand Down Expand Up @@ -652,16 +650,12 @@ void MainWindow::copyFullPath()

void MainWindow::openContainingFolder()
{
const auto& current_file = session_.getFilename( currentCrawlerWidget() );
const auto& dir = QFileInfo( current_file ).absolutePath();
QDesktopServices::openUrl( QUrl( QDir::toNativeSeparators( dir ) ) );
showPathInFileExplorer( session_.getFilename( currentCrawlerWidget() ) );
}


void MainWindow::openInEditor()
{
const auto& current_file = session_.getFilename( currentCrawlerWidget() );
QDesktopServices::openUrl( QUrl( QDir::toNativeSeparators( current_file ) ) );
openFileInDefaultApplication( session_.getFilename( currentCrawlerWidget() ) );
}

void MainWindow::onClipboardDataChanged()
Expand Down
23 changes: 10 additions & 13 deletions src/ui/src/tabbedcrawlerwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@

#include "tabbedcrawlerwidget.h"

#include <QApplication>
#include <QClipboard>
#include <QDir>
#include <QFileInfo>
#include <QKeyEvent>
#include <QLabel>
#include <QMenu>
#include <QFileInfo>
#include <QDir>
#include <QApplication>
#include <QClipboard>
#include <QDesktopServices>

#include "crawlerwidget.h"

#include "log.h"
#include "openfilehelper.h"

TabbedCrawlerWidget::TabbedCrawlerWidget()
: QTabWidget()
Expand Down Expand Up @@ -68,7 +68,7 @@ TabbedCrawlerWidget::TabbedCrawlerWidget()
&TabbedCrawlerWidget::showContextMenu );
}

void TabbedCrawlerWidget::addTabBarItem(int index, const QString &file_name)
void TabbedCrawlerWidget::addTabBarItem( int index, const QString& file_name )
{
const auto tab_label = QFileInfo( file_name ).fileName();

Expand Down Expand Up @@ -163,14 +163,11 @@ void TabbedCrawlerWidget::showContextMenu( const QPoint& point )
closeRight->setDisabled( true );
}

connect( copyFullPath, &QAction::triggered, [this, tab] {
QApplication::clipboard()->setText( tabToolTip( tab ) );
} );
connect( copyFullPath, &QAction::triggered,
[this, tab] { QApplication::clipboard()->setText( tabToolTip( tab ) ); } );

connect( openContainingFolder, &QAction::triggered, [this, tab] {
const auto& dir = QFileInfo( tabToolTip( tab ) ).absolutePath();
QDesktopServices::openUrl( QUrl( dir ) );
} );
connect( openContainingFolder, &QAction::triggered,
[this, tab] { showPathInFileExplorer( tabToolTip( tab ) ); } );

menu.exec( myTabBar_.mapToGlobal( point ) );
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ add_library(utils STATIC
${CMAKE_CURRENT_SOURCE_DIR}/include/perfcounter.h
${CMAKE_CURRENT_SOURCE_DIR}/include/persistable.h
${CMAKE_CURRENT_SOURCE_DIR}/include/persistentinfo.h
)
${CMAKE_CURRENT_SOURCE_DIR}/include/openfilehelper.h)

target_include_directories(utils PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(utils PUBLIC Qt5::Gui absl::optional plog)
89 changes: 89 additions & 0 deletions src/utils/include/openfilehelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (C) 2016 -- 2019 Anton Filimonov
*
* This file is part of klogg.
*
* klogg 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 3 of the License, or
* (at your option) any later version.
*
* klogg 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 klogg. If not, see <http://www.gnu.org/licenses/>.
*
* showPathInFileExplorer code is derived from Qt Creator sources
*/

/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#pragma once

#include <QApplication>
#include <QDir>
#include <QFileInfo>
#include <QUrl>
#include <QtCore/QProcess>
#include <QtGui/QDesktopServices>

#include "log.h"

inline void showPathInFileExplorer( const QString& file_path )
{
const QFileInfo file_info( file_path );

LOG( logINFO ) << "Show path in explorer: " << file_path;

#if defined( Q_OS_WIN )
const auto explorer = QString( "explorer.exe /select \"%1\"" )
.arg( QDir::toNativeSeparators( file_info.canonicalFilePath() ) );
QProcess::startDetached( explorer );
#elif defined( Q_OS_MAC )
QStringList scriptArgs;
scriptArgs << QLatin1String( "-e" )
<< QString::fromLatin1( "tell application \"Finder\" to reveal POSIX file \"%1\"" )
.arg( file_info.canonicalFilePath() );
QProcess::execute( QLatin1String( "/usr/bin/osascript" ), scriptArgs );
scriptArgs.clear();
scriptArgs << QLatin1String( "-e" )
<< QLatin1String( "tell application \"Finder\" to activate" );
QProcess::execute( QLatin1String( "/usr/bin/osascript" ), scriptArgs );
#else
QDesktopServices::openUrl( QUrl::fromLocalFile( file_info.canonicalPath() ) );
#endif
}

inline void openFileInDefaultApplication( const QString& file_path )
{
LOG( logINFO ) << "Open file in default app: " << file_path;

const QFileInfo file_info( file_path );
QDesktopServices::openUrl( QUrl::fromLocalFile( file_info.canonicalFilePath() ) );
}

0 comments on commit 373352c

Please sign in to comment.