Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #148 - Open documents into new tab rather than new window #476

Merged
merged 1 commit into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ReText/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
QFileInfo, QIODevice, QLibraryInfo, QTextStream, QTranslator, Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtNetwork import QNetworkProxyFactory
from PyQt5.QtDBus import QDBusConnection, QDBusInterface

def canonicalize(option):
if option in ('--preview', '-'):
Expand Down Expand Up @@ -74,7 +75,10 @@ def main():
parser.addVersionOption()
previewOption = QCommandLineOption('preview',
QApplication.translate('main', 'Open the files in preview mode'))
newWindowOption = QCommandLineOption('new-window',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know of this class in Qt, my fault

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also Python's argparse module, but Qt's is better, as it supports passing some standard flags (like -style or -platform) to Qt.

QApplication.translate('main', 'Create a new window even if there is an existing one'))
parser.addOption(previewOption)
parser.addOption(newWindowOption)
parser.addPositionalArgument('files',
QApplication.translate('main', 'List of files to open'),
'[files...]')
Expand All @@ -100,6 +104,20 @@ def main():
app.setStyleSheet(QTextStream(sheetfile).readAll())
sheetfile.close()
window = ReTextWindow()

connection = QDBusConnection.sessionBus()
if connection.isConnected() and not parser.isSet(newWindowOption):
connection.registerObject('/', window, QDBusConnection.ExportAllSlots)
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
serviceName = 'me.mitya57.ReText'
if not connection.registerService(serviceName) and filesToOpen:
print('Opening the file(s) in the existing window of ReText.')
iface = QDBusInterface(serviceName, '/', '', connection)
for fileName in filesToOpen:
iface.call('openFileWrapper', fileName)
qWidgetIface = QDBusInterface(serviceName, '/', 'org.qtproject.Qt.QWidget', connection)
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
qWidgetIface.call('raise')
sys.exit(0)

window.show()
# ReText can change directory when loading files, so we
# need to have a list of canonical names before loading
Expand Down
3 changes: 2 additions & 1 deletion ReText/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
enchant = None

from PyQt5.QtCore import QDir, QFile, QFileInfo, QFileSystemWatcher, \
QIODevice, QLocale, QMarginsF, QTextCodec, QTextStream, QTimer, QUrl, Qt
QIODevice, QLocale, QMarginsF, QTextCodec, QTextStream, QTimer, QUrl, Qt, pyqtSlot
from PyQt5.QtGui import QColor, QDesktopServices, QIcon, \
QKeySequence, QPageLayout, QPageSize, QPagedPaintDevice, QPalette, \
QTextDocument, QTextDocumentWriter
Expand Down Expand Up @@ -826,6 +826,7 @@ def openFile(self):
for fileName in fileNames[0]:
self.openFileWrapper(fileName)

@pyqtSlot(str)
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
def openFileWrapper(self, fileName):
if not fileName:
return
Expand Down