[Solved] Help firing AboutDialog::on_copyButton_clicked() wanted by n00b

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Sam
Posts: 177
Joined: Sun Mar 20, 2016 6:19 pm

[Solved] Help firing AboutDialog::on_copyButton_clicked() wanted by n00b

Post by Sam »

Code: Select all

OS: Ubuntu 16.04.3 LTS
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.11797 (Git)
Build type: None
Branch: master
Hash: a5080329d8da6653495df686efbda62e73ac950a
Python version: 2.7.12
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 7.1.0
Locale: English/UnitedKingdom (en_GB)
I've been asking for a lot of help lately. Today I spent the afternoon reading the source and docco to try and make a script that'd copy my version info to the clipboard with one click instead of menu>click>click, then click to dismiss.

I have failed. I see the method; it's AboutDialog::on_copyButton_clicked() in Splashscreen.cpp

But between knowing SFA about Python, and less about C++ I haven't managed to even find it in the available namespace.

From the wiki I try

Code: Select all

toplevel = QtGui.qApp.topLevelWidgets()
for i in toplevel:
    print i.metaObject().className()
And I deduce that it hangs off Gui::MainWindow somewhere. But I give up for now. It's time for a beer, and I know that for someone who's done it before this will be a 1 minute reply.

All I really want is a clue about how to call it. If you're interested, what I'd like to end up with is a button on my global toolbar that calls it and wraps the output in code tags ready to be pasted into a new post.
Last edited by Sam on Mon Aug 21, 2017 12:03 am, edited 1 time in total.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Help firing AboutDialog::on_copyButton_clicked() wanted by n00b

Post by Kunda1 »

+1
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Help firing AboutDialog::on_copyButton_clicked() wanted by n00b

Post by wmayer »

Code: Select all

from PySide import QtCore
from PySide import QtGui

class AboutInfo(QtCore.QObject):
  def eventFilter(self, obj, ev):
    if obj.metaObject().className() == "Gui::Dialog::AboutDialog":
      if ev.type() == ev.ChildPolished:
        print(obj.metaObject().className())
        mo = obj.metaObject()
        index = mo.indexOfMethod("on_copyButton_clicked()")
        if index > 0:
          mo.invokeMethod(obj, "on_copyButton_clicked")
          QtGui.qApp.postEvent(obj, QtGui.QCloseEvent())
    
    return False

ai=AboutInfo()
QtGui.qApp.installEventFilter(ai)
Gui.runCommand("Std_About")
QtGui.qApp.removeEventFilter(ai)
Sam
Posts: 177
Joined: Sun Mar 20, 2016 6:19 pm

Re: Help firing AboutDialog::on_copyButton_clicked() wanted by n00b

Post by Sam »

:D :D :D

Crikey wmayer! No wonder I failed. Thanks for making the effort.

Anyway, I've added a couple of extra lines to wrap the version info in code tags. If you use X it also copies it to the mouse selection for your middle clicking enjoyment.

Code: Select all

from PySide import QtCore
from PySide import QtGui

class AboutInfo(QtCore.QObject):
  def eventFilter(self, obj, ev):
    if obj.metaObject().className() == "Gui::Dialog::AboutDialog":
      if ev.type() == ev.ChildPolished:
        print(obj.metaObject().className())
        mo = obj.metaObject()
        index = mo.indexOfMethod("on_copyButton_clicked()")
        if index > 0:
          mo.invokeMethod(obj, "on_copyButton_clicked")
          QtGui.qApp.postEvent(obj, QtGui.QCloseEvent())
    
    return False

ai=AboutInfo()
QtGui.qApp.installEventFilter(ai)
Gui.runCommand("Std_About")
QtGui.qApp.removeEventFilter(ai)

clip = QtGui.qApp.clipboard()
if clip.mimeData(clip.Clipboard).hasText():
  versionInfo = clip.text(clip.Clipboard)
  versionInfo = "[code]"+versionInfo+"[/"+"code]\n"
  print versionInfo
  clip.setText(versionInfo,clip.Clipboard)
# mouse selection for the cool kids...
  if clip.supportsSelection():
    clip.setText(versionInfo,clip.Selection)
FWIW, the reasoning behind calling this, and not simply collecting and collect the info, was to stay consistent with any changes in the code.

Aside: I can not understand the first three lines of your class definition. Should I take the time to learn?

Edit: Don't know how to escape bbcode labels. Modified string in code to display properly on the forum...
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Help firing AboutDialog::on_copyButton_clicked() wanted by n00b

Post by wmayer »

Aside: I can not understand the first three lines of your class definition. Should I take the time to learn?
Yes.

FYI, in Qt any sub-class of QObject can re-implement the (in C++ virtual) method eventFilter() and can be installed to any other QObject-dervied instance to observe or filter the events it gets.
In C++ the class name of the About dialog including name spaces is called Gui::Dialog::AboutDialog and the class name of a QObject can be determined by its meta object. So, the event filter only looks for About dialogs and as event it uses the polish event which is called before the dialog is shown.

The name of the method of the dialog that is invoked when pressing the Copy button is called on_copyButton_clicked() and is marked as a slot function (for more information refer to the Qt docs to see what a slot function exactly is) and this allows it to allow to call it over its meta object.

By calling on_copyButton_clicked all the needed information is copied to the clipboard and the dialog can be closed again. Thus, a close event is posted to the dialog class.

The four lines at the end are needed to install the event filter to the global application object, invoke the Std_About command which internally creates the About dialog and removing the event filter again.
Sam
Posts: 177
Joined: Sun Mar 20, 2016 6:19 pm

Re: Help firing AboutDialog::on_copyButton_clicked() wanted by n00b

Post by Sam »

wmayer wrote: Sun Aug 20, 2017 9:11 pmFYI...
That is an extensive explanation. After googling a bit, and looking at the source, I understand.

Thank you for taking the time.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: [Solved] Help firing AboutDialog::on_copyButton_clicked() wanted by n00b

Post by Kunda1 »

I wish I could 'Star* certain posts.
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Help firing AboutDialog::on_copyButton_clicked() wanted by n00b

Post by Kunda1 »

wmayer wrote: Sun Aug 20, 2017 2:00 pm

Code: Select all

from PySide import QtCore
from PySide import QtGui

class AboutInfo(QtCore.QObject):
  def eventFilter(self, obj, ev):
    if obj.metaObject().className() == "Gui::Dialog::AboutDialog":
      if ev.type() == ev.ChildPolished:
        print(obj.metaObject().className())
        mo = obj.metaObject()
        index = mo.indexOfMethod("on_copyButton_clicked()")
        if index > 0:
          mo.invokeMethod(obj, "on_copyButton_clicked")
          QtGui.qApp.postEvent(obj, QtGui.QCloseEvent())
    
    return False

ai=AboutInfo()
QtGui.qApp.installEventFilter(ai)
Gui.runCommand("Std_About")
QtGui.qApp.removeEventFilter(ai)
We should do something with this script. I keep thinking about it and that it can be useful for something.
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
Sam
Posts: 177
Joined: Sun Mar 20, 2016 6:19 pm

Re: [Solved] Help firing AboutDialog::on_copyButton_clicked() wanted by n00b

Post by Sam »

It's useful for this:

Code: Select all

OS: Ubuntu 16.04.5 LTS
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.14893 (Git)
Build type: Release
Branch: master
Hash: b8c50dc0f93410d1a8009a162385b50107c301ec
Python version: 2.7.12
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/UnitedKingdom (en_GB)
I've been distracted for ages now, but it's saved me a bunch of time, and stoopid posts, by making it easy to include version info.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: [Solved] Help firing AboutDialog::on_copyButton_clicked() wanted by n00b

Post by Kunda1 »

chrisb wrote::bell:
@chrisb this is what we were looking for. Need to find the thread where it was discussed to make a command that circumvents Help > About FreeCAD > Copy to clipboard
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
Post Reply