Commit d735d50c authored by Andi Sardina Ramos's avatar Andi Sardina Ramos Committed by Albert Astals Cid
Browse files

Give the option of switching to a tab if the file is already open instead of opening a new tab

Disabled by default

BUG: 419096
parent 5410a798
Pipeline #18821 passed with stage
in 7 minutes and 58 seconds
......@@ -95,6 +95,7 @@ private slots:
void testSessionRestore();
void testOpenInvalidFiles_data();
void testOpenInvalidFiles();
void testOpenTheSameFileSeveralTimes();
private:
};
......@@ -670,6 +671,41 @@ void MainShellTest::testOpenInvalidFiles()
}
void MainShellTest::testOpenTheSameFileSeveralTimes()
{
QString options = ShellUtils::serializeOptions( false, false, false, false, false, QString(), QString() );
Okular::Settings::self()->setShellOpenFileInTabs( true );
Okular::Status status = Okular::main( QStringList(), options );
QCOMPARE( status, Okular::Success );
Shell *shell = findShell();
QVERIFY( shell );
QUrl file1 = ShellUtils::urlFromArg( QStringLiteral( KDESRCDIR "data/file1.pdf" ), ShellUtils::qfileExistFunc(), QString() );
QUrl file2 = ShellUtils::urlFromArg( QStringLiteral( KDESRCDIR "data/file2.pdf" ), ShellUtils::qfileExistFunc(), QString() );
QUrl file3 = ShellUtils::urlFromArg( QStringLiteral( KDESRCDIR "data/formattest.pdf" ), ShellUtils::qfileExistFunc(), QString() );
shell->openUrl( file1 );
shell->openUrl( file2 );
shell->openUrl( file2 );
QVERIFY( shell->m_tabs.size() == 3 );
Okular::Settings::self()->setSwitchToTabIfOpen( true );
shell->openUrl( file3 );
shell->openUrl( file1 );
QVERIFY( shell->m_tabWidget->currentIndex() == 0 );
shell->openUrl( file3 );
QVERIFY( shell->m_tabWidget->currentIndex() == 3 );
QVERIFY( shell->m_tabs.size() == 4 );
}
void MainShellTest::testMiddleButtonCloseUndo()
{
const QStringList paths = { QStringLiteral(KDESRCDIR "data/file1.pdf"), QStringLiteral(KDESRCDIR "data/file2.pdf") };
......
......@@ -22,8 +22,6 @@ DlgGeneral::DlgGeneral( QWidget * parent, Okular::EmbedMode embedMode )
m_dlg = new Ui_DlgGeneralBase();
m_dlg->setupUi( this );
setCustomBackgroundColorButton( Okular::Settings::useCustomBackgroundColor() );
if( embedMode == Okular::ViewerWidgetMode )
{
m_dlg->kcfg_SyncThumbnailsViewport->setVisible( false );
......@@ -31,9 +29,13 @@ DlgGeneral::DlgGeneral( QWidget * parent, Okular::EmbedMode embedMode )
m_dlg->kcfg_WatchFile->setVisible( false );
m_dlg->kcfg_rtlReadingDirection->setVisible(false);
}
m_dlg->kcfg_BackgroundColor->setEnabled( Okular::Settings::useCustomBackgroundColor() );
m_dlg->kcfg_ShellOpenFileInTabs->setVisible( embedMode == Okular::NativeShellMode );
m_dlg->kcfg_SwitchToTabIfOpen->setEnabled( Okular::Settings::shellOpenFileInTabs() );
connect(m_dlg->kcfg_UseCustomBackgroundColor, &QCheckBox::toggled, this, &DlgGeneral::setCustomBackgroundColorButton);
connect( m_dlg->kcfg_UseCustomBackgroundColor, &QCheckBox::toggled, m_dlg->kcfg_BackgroundColor, &QCheckBox::setEnabled );
connect( m_dlg->kcfg_ShellOpenFileInTabs, &QCheckBox::toggled, m_dlg->kcfg_SwitchToTabIfOpen, &QCheckBox::setEnabled );
}
DlgGeneral::~DlgGeneral()
......@@ -53,7 +55,3 @@ void DlgGeneral::showEvent( QShowEvent * )
#endif
}
void DlgGeneral::setCustomBackgroundColorButton( bool value )
{
m_dlg->kcfg_BackgroundColor->setEnabled( value );
}
......@@ -24,9 +24,6 @@ class DlgGeneral : public QWidget
explicit DlgGeneral( QWidget * parent, Okular::EmbedMode embedMode );
~DlgGeneral() override;
public slots:
void setCustomBackgroundColorButton( bool value );
protected:
void showEvent( QShowEvent * ) override;
......
......@@ -10,7 +10,7 @@
<x>0</x>
<y>0</y>
<width>558</width>
<height>632</height>
<height>635</height>
</rect>
</property>
<layout class="QVBoxLayout">
......@@ -232,39 +232,9 @@
<property name="title">
<string>Program Features</string>
</property>
<layout class="QHBoxLayout">
<property name="spacing">
<number>6</number>
</property>
<property name="leftMargin">
<number>9</number>
</property>
<property name="topMargin">
<number>9</number>
</property>
<property name="rightMargin">
<number>9</number>
</property>
<property name="bottomMargin">
<number>9</number>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<layout class="QVBoxLayout">
<property name="spacing">
<number>6</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QCheckBox" name="kcfg_ShellOpenFileInTabs">
<property name="text">
......@@ -272,6 +242,33 @@
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>25</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QCheckBox" name="kcfg_SwitchToTabIfOpen">
<property name="text">
<string>Switch to existing tab if file is already open</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="kcfg_ObeyDRM">
<property name="text">
......@@ -480,4 +477,5 @@ For files which were opened before the previous zoom is applied.</string>
<include location="global">kiconloader.h</include>
</includes>
<resources/>
<connections/>
</ui>
......@@ -128,6 +128,9 @@
<entry key="ShellOpenFileInTabs" type="Bool" >
<default>false</default>
</entry>
<entry key="SwitchToTabIfOpen" type="Bool">
<default>false</default>
</entry>
<entry key="ShowOSD" type="Bool" >
<default>true</default>
</entry>
......
......@@ -74,7 +74,7 @@ class VIEWERINTERFACE_EXPORT ViewerInterface
virtual void setWatchFileModeEnabled(bool b) = 0;
/**
* Should the shell that supports tabs pen new files in tabs?
* Should the shell that supports tabs open new files in tabs?
*/
virtual bool openNewFilesInTabs() const = 0;
......
......@@ -1095,6 +1095,11 @@ bool Part::openNewFilesInTabs() const
return Okular::Settings::self()->shellOpenFileInTabs();
}
bool Part::activateTabIfAlreadyOpenFile() const
{
return Okular::Settings::self()->switchToTabIfOpen();
}
void Part::slotHandleActivatedSourceReference(const QString& absFileName, int line, int col, bool *handled)
{
emit openSourceReference( absFileName, line, col );
......
......@@ -145,6 +145,7 @@ class OKULARPART_EXPORT Part : public KParts::ReadWritePart, public Okular::Docu
bool areSourceLocationsShownGraphically() const override;
void setShowSourceLocationsGraphically(bool show) override;
bool openNewFilesInTabs() const override;
Q_INVOKABLE bool activateTabIfAlreadyOpenFile() const;
public Q_SLOTS: // dbus
Q_SCRIPTABLE Q_NOREPLY void goToPage(uint page) override;
......
......@@ -674,6 +674,23 @@ void Shell::closeTab( int tab )
void Shell::openNewTab( const QUrl& url, const QString &serializedOptions )
{
const int previousActiveTab = m_tabWidget->currentIndex();
KParts::ReadWritePart* const activePart = m_tabs[previousActiveTab].part;
bool activateTabIfAlreadyOpen;
QMetaObject::invokeMethod( activePart, "activateTabIfAlreadyOpenFile", Q_RETURN_ARG( bool, activateTabIfAlreadyOpen ) );
if( activateTabIfAlreadyOpen ){
const int tabIndex = findTabIndex( url );
if ( tabIndex >= 0 ) {
setActiveTab( tabIndex );
m_recent->addUrl( url );
return;
}
}
// Tabs are hidden when there's only one, so show it
if( m_tabs.size() == 1 )
{
......@@ -694,7 +711,6 @@ void Shell::openNewTab( const QUrl& url, const QString &serializedOptions )
applyOptionsToPart(part, serializedOptions);
int previousActiveTab = m_tabWidget->currentIndex();
setActiveTab( m_tabs.size() - 1 );
if( part->openUrl(url) )
......@@ -809,7 +825,7 @@ void Shell::setTabIcon( const QMimeType& mimeType )
}
}
int Shell::findTabIndex( QObject* sender )
int Shell::findTabIndex( QObject* sender ) const
{
for( int i = 0; i < m_tabs.size(); ++i )
{
......@@ -821,6 +837,16 @@ int Shell::findTabIndex( QObject* sender )
return -1;
}
int Shell::findTabIndex(const QUrl &url ) const
{
auto it = std::find_if( m_tabs.begin(), m_tabs.end(),
[&url]( const TabState state ){
return state.part->url() == url;
});
return ( it != m_tabs.end() ) ? std::distance( m_tabs.begin(), it ) : -1;
}
void Shell::handleDroppedUrls( const QList<QUrl>& urls )
{
for ( const QUrl &url : urls )
......
......@@ -140,7 +140,8 @@ private:
void openNewTab( const QUrl& url, const QString &serializedOptions );
void applyOptionsToPart( QObject* part, const QString &serializedOptions );
void connectPart( QObject* part );
int findTabIndex( QObject* sender );
int findTabIndex( QObject* sender ) const;
int findTabIndex( const QUrl& url ) const;
private:
bool eventFilter(QObject *obj, QEvent *event) override;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment