from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.core.clipboard import Clipboard
import webbrowser
import six.moves.urllib
import os

Builder.load_string('''
<BugReporter>:
    txt_traceback: txt_traceback
    Image:
        source: 'data/logo/kivy-icon-256.png'
        opacity: 0.2
    BoxLayout:
        orientation: 'vertical'
        padding: 10
        Label:
            id: title
            text: 'Sorry, Kivy Designer has experienced an internal error :('
            font_size: '16pt'
            halign: 'center'
            size_hint_y: None
            height: '30pt'
        Label:
            id: subtitle
            text: 'You can report this bug using the button bellow, ' \
                    'helping us to fix it.'
            text_size: self.size
            font_size: '11pt'
            halign: 'center'
            valign: 'top'
            size_hint_y: None
            height: '30pt'
        ScrollView:
            id: e_scroll
            bar_width: 10
            scroll_y: 0
            TextInput:
                id: txt_traceback
                size_hint_y: None
                height: max(e_scroll.height, self.minimum_height)
                background_color: 1, 1, 1, 0.05
                text: ''
                foreground_color: 1, 1, 1, 1
                readonly: True
        BoxLayout:
            size_hint: 0.6, None
            padding: 10, 10
            height: 50
            pos_hint: {'x':0.2}
            spacing: 5
            Button:
                text: 'Copy to clipboard'
                on_press: root.on_clipboard()
            Button:
                text: 'Report Bug'
                on_press: root.on_report()
            Button:
                text: 'Close'
                on_press: root.on_close()
''')


class BugReporter(FloatLayout):
    txt_traceback = ObjectProperty(None)
    '''TextView to show the traceback message
    '''

    def on_clipboard(self, *args):
        '''Event handler to "Copy to Clipboard" button
        '''
        Clipboard.copy(self.txt_traceback.text)

    def on_report(self, *args):
        '''Event handler to "Report Bug" button
        '''
        txt = six.moves.urllib.parse.quote(self.txt_traceback.text)
        url = 'https://github.com/kivy/kivy-designer/issues/new?body=' + txt
        webbrowser.open(url)

    def on_close(self, *args):
        '''Event handler to "Close" button
        '''
        App.get_running_app().stop()


class BugReporterApp(App):

    title = "Kivy Designer - Bug reporter"
    traceback = ''

    def __init__(self, traceback):
        self.traceback = traceback
        super(BugReporterApp, self).__init__()

    def build(self):
        rep = BugReporter()
        template = '''
## Environment Info

%s

## Traceback

%s

'''
        env_info = 'Pip is not installed'
        try:
            from pip.req import parse_requirements
            from pip.download import PipSession
            import platform

            requirements = parse_requirements(os.path.join(
                os.path.dirname(os.path.realpath(__file__)),
                '..',
                '..',
                'requirements.txt'),
                session=PipSession()
            )
            env_info = ''
            for req in requirements:
                version = req.installed_version
                if version is None:
                    version = 'Not installed'
                env_info += req.name + ': ' + version + '\n'
            env_info += '\nPlatform: ' + platform.platform()
            env_info += '\nPython: ' + platform.python_version()

        except ImportError:
            pass
        rep.txt_traceback.text = template % (env_info, self.traceback)

        return rep


if __name__ == '__main__':
    BugReporterApp('Bug example').run()

@@ -65,16 +65,32 @@
 Property
+, StringProperty
 %0Afrom ki
@@ -247,16 +247,83 @@
 ort os%0A%0A
+from kivy.uix.label import Label%0Afrom kivy.uix.popup import Popup%0A%0A
 Builder.
@@ -1954,12 +1954,607 @@
 e()%0A
-''')
+%0A%3CReportWarning%3E:%0A    size_hint: .5, .5%0A    auto_dismiss: False%0A    title: 'Warning'%0A    BoxLayout:%0A        orientation: 'vertical'%0A        Label:%0A            text_size: self.size%0A            text: root.text%0A            padding: '4sp', '4sp'%0A            valign: 'middle'%0A        Button:%0A            size_hint_y: None%0A            height: '40sp'%0A            on_release: root.dispatch('on_release')%0A            text: 'Report'%0A''')%0A%0A%0Aclass ReportWarning(Popup):%0A    text = StringProperty('')%0A    '''Warning Message%0A    '''%0A%0A    __events__ = ('on_release',)%0A%0A    def on_release(self, *args):%0A        pass
 %0A%0A%0Ac
@@ -2676,24 +2676,131 @@
 ge%0A    '''%0A%0A
+    def __init__(self, **kw):%0A        super(BugReporter, self).__init__(**kw)%0A        self.warning = None%0A%0A
     def on_c
@@ -3022,32 +3022,602 @@
 ton%0A        '''%0A
+        warning = ReportWarning()%0A        warning.text = ('Warning. Some web browsers doesn%5C't post the full'%0A                        ' traceback error. %5Cn%5CnPlease, check if the last line'%0A                        ' of your report is %22End of Traceback%22. %5Cn%5Cn'%0A                        'If not, use the %22Copy to clipboard%22 button the get'%0A                        'the full report and post it manually.%22')%0A        warning.bind(on_release=self._do_report)%0A        warning.open()%0A        self.warning = warning%0A%0A    def _do_report(self, *args):%0A        self.warning.dismiss()%0A
         txt = si
@@ -3927,17 +3927,16 @@
 p(App):%0A
-%0A
     titl
@@ -4223,16 +4223,33 @@
 ck%0A%0A%25s%0A%0A
+End of Traceback%0A
 '''%0A    

add warning to bug reporter