class ComponentSearchForm():

    data_source = None
    
    related_mashups = None
    
    widget = None
    
    table = None
    
    add_btn = None
    
    graph_form = None
    
    highlighted_api = None
    
    highlighted_mashup = None
    
    def show_main_window(self):
        self.graph_form = matplotlibWidget()
        if self.widget:
            self.widget.destroy()
            self.widget = None;
        self.widget = QWidget()
        self.widget.setMinimumSize(800, 600)
        btn_api = QtGui.QPushButton("Recommend Modules", self.widget)
        btn_api.move(30, 20)

        btn_mashup = QtGui.QPushButton("Recommend Workflows", self.widget)
        btn_mashup.move(200, 20)

        self.textboxLabel = QLabel(self.widget)
        self.textboxLabel.setText("Describe your goals:")
        self.textboxLabel.move(35, 60)
        self.textboxLabel.show
        
        self.textbox = QTextEdit(self.widget)
        self.textbox.move(30, 80)
        self.textbox.setFixedWidth(300)
        self.textbox.setFixedHeight(28)

        btn_api.clicked.connect(self.api_button_clicked)
        btn_mashup.clicked.connect(self.mashups_button_clicked)

        self.table = QTableView(self.widget)
        self.table.clicked.connect(self.table_clicked)
        self.table.setMinimumSize(740, 500)
        self.table.resizeColumnsToContents()
        self.table.move(30, 120)

        self.widget.show()
        
        self.add_btn = QPushButton(self.widget)
        self.add_btn.clicked.connect(self.add_new_api)
        self.add_btn.setText("Add to Palette")
        self.add_btn.hide()
        self.add_btn.move(650, 20)
        
        self.recommendLabel = QLabel(self.widget)
        self.recommendLabel.setText("Also Used")
        self.recommendLabel.move(30, 95)
        self.recommendLabel.hide()
        
        self.switch_btn_apis = QPushButton(self.widget)
        self.switch_btn_apis.clicked.connect(self._show_related_apis)
        self.switch_btn_apis.setText("Related Mashup")
        self.switch_btn_apis.move(500, 20)
        self.switch_btn_apis.hide()
        
        self.switch_btn_mashups = QPushButton(self.widget)
        self.switch_btn_mashups.clicked.connect(self._show_related_mashups)
        self.switch_btn_mashups.setText("Related API")
        self.switch_btn_mashups.move(500, 20)
        self.switch_btn_mashups.hide()

    def __init__(self, parent=None):
        self.data_source = DataSource()
        
    def table_clicked(self):
        """
        Click the table, the graph form may change according to the selection.
        """
        model = self.table.selectionModel()
        indexes = model.selectedIndexes()
        for index in indexes:
            row = index.row()
            data = index.model().headerData(0,Qt.Horizontal).toString()
            newIndex = index.model().index(row, 0)
            if data == "API":
                api_id = get_api_full_name(newIndex.model().data(newIndex).toString())
                api = self.data_source.api_by_id(api_id)
                print api
                mashups = self.data_source.mashups_by_api(api)
                apis = []
                for mashup in mashups:
                    apis.extend(self.data_source.apis_by_mashup(mashup))
                self.graph_form.draw_apis(apis, api, self.highlighted_api)
            else:
                mashup_id = get_mashup_full_name(newIndex.model().data(newIndex).toString())
                mashup = self.data_source.mashup_by_id(mashup_id)
                if not mashup:
                    return
                apis = self.data_source.apis_by_mashup(mashup)
                mashups = []
                if len(apis) > 0:
                    mashups.extend(self.data_source.mashups_by_api(apis[0]))
                self.graph_form.draw_mashups(mashups, mashup, self.highlighted_mashup)
            return


    def _show_apis(self, apis, recommend=False):
        self.switch_btn_apis.hide()
        self.switch_btn_mashups.hide()
        self.recommendLabel.hide()

        row = 0
        model = QStandardItemModel(len(apis), 4)
        model.setColumnCount(4)
        for api in apis:
            model.setData(model.index(row, 0), QVariant(get_api_name(api)))
            model.setData(model.index(row, 1), QVariant(api['protocols']))
            model.setData(model.index(row, 2), QVariant(api['provider']))
            model.setData(model.index(row, 3), QVariant(api['version']))
            row += 1

        model.setHeaderData(0, Qt.Horizontal, QVariant("Module"))
        model.setHeaderData(1, Qt.Horizontal, QVariant("Protocol"))
        model.setHeaderData(2, Qt.Horizontal, QVariant("Provider"))
        model.setHeaderData(3, Qt.Horizontal, QVariant("Version"))

#        model.setHeaderData(0, Qt.Horizontal, QVariant("API"))
#        model.setHeaderData(1, Qt.Horizontal, QVariant("Protocols"))
#        model.setHeaderData(2, Qt.Horizontal, QVariant("Provider"))
#        model.setHeaderData(3, Qt.Horizontal, QVariant("Version"))

        self.table.setModel(model)
        self.table.resizeColumnsToContents()
        
        if recommend:
            self.recommendLabel.show()
        
        self.add_btn.show()

    def _show_mashups(self, mashups):
        self.switch_btn_apis.hide()
        self.switch_btn_mashups.hide()
        self.recommendLabel.hide()

        row = 0
        model = QStandardItemModel(len(mashups), 4)
        model.setColumnCount(4)
        for mashup in mashups:
            model.setData(model.index(row, 0), QVariant(get_mashup_name(mashup)))
            model.setData(model.index(row, 1), QVariant(mashup['title']))
            model.setData(model.index(row, 2), QVariant(mashup['self']))
            model.setData(model.index(row, 3), QVariant(mashup['description']))
            
            row += 1
        
        model.setHeaderData(0, Qt.Horizontal, QVariant("Workflow"))
        model.setHeaderData(1, Qt.Horizontal, QVariant("Short Description"))
        model.setHeaderData(2, Qt.Horizontal, QVariant("Provider"))
        model.setHeaderData(3, Qt.Horizontal, QVariant("Detailed Info"))

#        model.setHeaderData(0, Qt.Horizontal, QVariant("Info"))
#        model.setHeaderData(1, Qt.Horizontal, QVariant("Title"))
#        model.setHeaderData(2, Qt.Horizontal, QVariant("self"))
#        model.setHeaderData(3, Qt.Horizontal, QVariant("Description"))

        self.table.setModel(model)
        self.table.resizeColumnsToContents()
        self.add_btn.show()

    def api_button_clicked(self):
        """
        Trigger to search APIs
        """        
        self.graph_form.draw_api()
        self.graph_form.show()

        apis = self.data_source.apis()
        key = str(self.textbox.toPlainText())
        #Has key or not has key, it is different.
        if key:
            self.api_search_button_clicked()
        else:
            self._show_apis(apis)

    def mashups_button_clicked(self):
        """
        Trigger to search mashups
        """
        self.graph_form.draw_mashup()
        self.graph_form.show()

        key = str(self.textbox.toPlainText())
        if key:
            self.mashup_search_button_clicked()
        else:
            self._show_mashups(self.data_source.mashups())

    #Should probably refactor this into one method.
    def api_search_button_clicked(self):
        """
        Search when no keyword
        """
        self.highlighted_api = None
        self.highlighted_mashup = None
        key = str(self.textbox.toPlainText())
        if key != "":
            apis = self.data_source.search_api(key)
            self._show_apis(apis)

    def mashup_search_button_clicked(self):
        """
        Search when no keyword
        """
        self.highlighted_api = None
        self.highlighted_mashup = None
        key = str(self.textbox.toPlainText())
        if key != "":
            mashups = self.data_source.search_mashup(key)
            self._show_mashups(mashups)
    
    def add_new_api(self):
        """
        Add new api to the modules package.
        """
        apis = self.data_source.apis()
        model = self.table.selectionModel()
        indexes = model.selectedIndexes()
        for index in indexes:
            api = apis[index.row()]
            self._add_new_api(api)
            return
    
    def add_related_api(self):
        objs = []
        for mashup in self.related_mashups:
            objs.append(mashup)
            for api in mashup["related_mashups"]:
                objs.append(api)
        model = self.table.selectionModel()
        indexes = model.selectedIndexes()
        for index in indexes:
            api = objs[index.row()]
            if api.get("protocols"):
                self._add_new_api(api)
                return

    def _show_related_mashups(self):
        self.switch_btn_apis.hide()
        self.switch_btn_mashups.hide()
        self.recommendLabel.hide()
        
        apis = []
        objs = []
        for mashup in self.related_mashups:
            apis.extend(mashup["related_mashups"])

        for api in apis:
            objs.append(api)
            mashups = self.data_source.mashups_by_api(api)
            objs.extend(mashups)

        row = 0
        model = QStandardItemModel(len(objs), 4)
        model.setColumnCount(4)
        for obj in objs:
            if obj.get('protocols'):
                model.setData(model.index(row, 0), QVariant(get_api_name(obj)))
                model.setData(model.index(row, 1), QVariant(obj['protocols']))
                model.setData(model.index(row, 2), QVariant(obj['provider']))
            else:
                model.setData(model.index(row, 3), QVariant(get_mashup_name(obj)))
            row += 1

        model.setHeaderData(0, Qt.Horizontal, QVariant("API"))
        model.setHeaderData(1, Qt.Horizontal, QVariant("Protocols"))
        model.setHeaderData(2, Qt.Horizontal, QVariant("Provider"))
        model.setHeaderData(3, Qt.Horizontal, QVariant("Mashup"))
        self.table.setModel(model)
        self.table.resizeColumnsToContents()
        self.switch_btn_apis.show()

    def _show_related_apis(self):
        self.switch_btn_apis.hide()
        self.switch_btn_mashups.hide()
        self.recommendLabel.hide()
        
        row = 0
        objs = []
        for mashup in self.related_mashups:
            objs.append(mashup)
            for api in mashup["related_mashups"]:
                objs.append(api)
        #Combining similarity and related.
        similar_apis = self.data_source.search_api_similarity(self.highlighted_api)
        #return str(mashup['id'])[(len("http://www.programmableweb.com/mashup/")):]
        objs.append({'id': "http://www.programmableweb.com/mashup/Using-Similarity-Metric"})
        objs.extend(similar_apis)
        #Combining similarity and related.

        model = QStandardItemModel(len(objs), 5)
        for obj in objs:
            if obj.get('protocols'):
                model.setData(model.index(row, 1), QVariant(get_api_name(obj)))
                model.setData(model.index(row, 2), QVariant(obj['protocols']))
                model.setData(model.index(row, 3), QVariant(obj['provider']))
                model.setData(model.index(row, 4), QVariant(obj['version']))
            else:
                model.setData(model.index(row, 0), QVariant(get_mashup_name(obj)))
            row += 1

        model.setHeaderData(0, Qt.Horizontal, QVariant("Mashup"))
        model.setHeaderData(1, Qt.Horizontal, QVariant("API"))
        model.setHeaderData(2, Qt.Horizontal, QVariant("Protocols"))
        model.setHeaderData(3, Qt.Horizontal, QVariant("Provider"))
        model.setHeaderData(4, Qt.Horizontal, QVariant("Version"))

        self.table.setModel(model)
        self.switch_btn_mashups.show()

    def _add_new_api(self, api):
        self.highlighted_api = api
        mashups = self.data_source.mashups_by_api(api)
        for mashup in mashups:
            mashup['related_mashups'] = (self.data_source.apis_by_mashup(mashup))
        if len(mashups) > 0:
            self.related_mashups = mashups
            self._show_related_apis()
        manager = core.packagemanager.get_package_manager()
        reg = core.modules.module_registry.get_module_registry()
        package = manager.get_package("edu.cmu.sv.components", "1.0.0")
        core.modules.module_registry.set_current_package(package)

        if (api["protocols"] == "SOAP" and api["wsdl"] != ""):
            s = Service(api["wsdl"])
            add_new_service(s, api["wsdl"])
        else:
            new_module = vistrails_module.new_module(Module, get_api_name(api))
            reg.add_module(new_module)
    
            reg.add_input_port(new_module, "value1",
                         (core.modules.basic_modules.String, 'the first argument'))
            reg.add_input_port(new_module, "value2",
                         (core.modules.basic_modules.String, 'the second argument'))
            reg.add_output_port(new_module, "value",
                          (core.modules.basic_modules.String, 'the result'))
Example #2
0
class OWxraylib_widget(widget.OWWidget):
    name = "Xraylib"
    id = "orange.widgets.dataxraylib_widget"
    description = "XRAYLIB data"
    icon = "icons/xraylib.png"
    author = "Manuel Sanchez del Rio, Luca Rebuffi "
    maintainer_email = "*****@*****.**"
    priority = 12
    category = ""
    keywords = ["xoppy", "xraylib_widget"]

    want_main_area = False

    FUNCTION = Setting(0)
    ELEMENT = Setting(26)
    ELEMENTORCOMPOUND = Setting("FeSO4")
    COMPOUND = Setting("Ca5(PO4)3")
    TRANSITION_IUPAC_OR_SIEGBAHN = Setting(1)
    TRANSITION_IUPAC_TO = Setting(0)
    TRANSITION_IUPAC_FROM = Setting(0)
    TRANSITION_SIEGBAHN = Setting(0)
    SHELL = Setting(0)
    ENERGY = Setting(10.0)

    MAX_WIDTH = 700
    MAX_HEIGHT = 700
    CONTROL_AREA_WIDTH = 690

    def __init__(self):
        super().__init__()

        geom = QApplication.desktop().availableGeometry()
        self.setGeometry(QRect(round(geom.width()*0.05),
                               round(geom.height()*0.05),
                               round(min(geom.width()*0.98, self.MAX_WIDTH)),
                               round(min(geom.height()*0.95, self.MAX_HEIGHT))))

        self.setMaximumHeight(self.geometry().height())
        self.setMaximumWidth(self.geometry().width())

        self.controlArea.setFixedWidth(self.CONTROL_AREA_WIDTH)

        box0 = gui.widgetBox(self.controlArea, "", orientation="horizontal")
        #widget buttons: compute, set defaults, help
        gui.button(box0, self, "Compute", callback=self.compute)
        gui.button(box0, self, "Defaults", callback=self.defaults)
        gui.button(box0, self, "Help", callback=self.help1)

        gui.separator(self.controlArea, height=10)

        box = oasysgui.widgetBox(self.controlArea, self.name + " Input Parameters",orientation="vertical", width=self.CONTROL_AREA_WIDTH-5, height=150)

        self.xoppy_output = QTextEdit()
        self.xoppy_output.setReadOnly(True)
        self.xoppy_output.setFixedHeight(440)
        self.xoppy_output.setFixedWidth(self.CONTROL_AREA_WIDTH-25)

        out_box = gui.widgetBox(self.controlArea, "Calculation Output", addSpace=True, orientation="horizontal")
        out_box.layout().addWidget(self.xoppy_output)

        idx = -1
        
        #widget index 0 
        idx += 1 
        box1 = gui.widgetBox(box) 
        gui.comboBox(box1, self, "FUNCTION",
                     label=self.unitLabels()[idx], addSpace=False,
                    items=['0 Fluorescence line energy', '1 Absorption edge energy', '2 Atomic weight', '3 Elemental density', '4 Total absorption cross section', '5 Photoionization cross section', '6 Partial photoionization cross section', '7 Rayleigh scattering cross section', '8 Compton scattering cross section', '9 Klein-Nishina cross section', '10 Mass energy-absorption cross section', '11 Differential unpolarized Klein-Nishina cross section', '12 Differential unpolarized Thomson cross section', '13 Differential unpolarized Rayleigh cross section', '14 Differential unpolarized Compton cross section', '15 Differential polarized Klein-Nishina cross section', '16 Differential polarized Thomson cross section', '17 Differential polarized Rayleigh cross section', '18 Differential polarized Compton cross section', '19 Atomic form factor', '20 Incoherent scattering function', '21 Momentum transfer function', '22 Coster-Kronig transition probability', '23 Fluorescence yield', '24 Jump factor', '25 Radiative transition probability', '26 Energy after Compton scattering', '27 Anomalous scattering factor φ′', '28 Anomalous scattering factor φ″', '29 Electronic configuration', '30 X-ray fluorescence production cross section (with full cascade)', '31 X-ray fluorescence production cross section (with radiative cascade)', '32 X-ray fluorescence production cross section (with non-radiative cascade)', '33 X-ray fluorescence production cross section (without cascade)', '34 Atomic level width', '35 Auger yield', '36 Auger rate', '37 Refractive index', '38 Compton broadening profile', '39 Partial Compton broadening profile', '40 List of NIST catalog compounds', '41 Get composition of NIST catalog compound', '42 List of X-ray emitting radionuclides', '43 Get excitation profile of X-ray emitting radionuclide', '44 Compoundparser'],
                    valueType=int, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1) 
        
        #widget index 1 
        idx += 1 
        box1 = gui.widgetBox(box) 
        oasysgui.lineEdit(box1, self, "ELEMENT",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=int, validator=QIntValidator(), orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1) 
        
        #widget index 2 
        idx += 1 
        box1 = gui.widgetBox(box) 
        oasysgui.lineEdit(box1, self, "ELEMENTORCOMPOUND",
                     label=self.unitLabels()[idx], addSpace=False, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1) 
        
        #widget index 3 
        idx += 1 
        box1 = gui.widgetBox(box) 
        oasysgui.lineEdit(box1, self, "COMPOUND",
                     label=self.unitLabels()[idx], addSpace=False, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1) 
        
        #widget index 4 
        idx += 1 
        box1 = gui.widgetBox(box) 
        gui.comboBox(box1, self, "TRANSITION_IUPAC_OR_SIEGBAHN",
                     label=self.unitLabels()[idx], addSpace=False,
                    items=['IUPAC', 'SIEGBAHN', 'ALL TRANSITIONS'],
                    valueType=int, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1) 
        
        #widget index 5 
        idx += 1 
        box1 = gui.widgetBox(box) 
        gui.comboBox(box1, self, "TRANSITION_IUPAC_TO",
                     label=self.unitLabels()[idx], addSpace=False,
                    items=['K', 'L1', 'L2', 'L3', 'M1', 'M2', 'M3', 'M4', 'M5', 'N1', 'N2', 'N3', 'N4', 'N5', 'N6', 'N7', 'O1', 'O2', 'O3', 'O4', 'O5', 'O6', 'O7', 'P1', 'P2', 'P3', 'P4', 'P5', 'Q1', 'Q2', 'Q3'],
                    valueType=int, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1) 
        
        #widget index 6 
        idx += 1 
        box1 = gui.widgetBox(box) 
        gui.comboBox(box1, self, "TRANSITION_IUPAC_FROM",
                     label=self.unitLabels()[idx], addSpace=False,
                    items=['K', 'L1', 'L2', 'L3', 'M1', 'M2', 'M3', 'M4', 'M5', 'N1', 'N2', 'N3', 'N4', 'N5', 'N6', 'N7', 'O1', 'O2', 'O3', 'O4', 'O5', 'O6', 'O7', 'P1', 'P2', 'P3', 'P4', 'P5', 'Q1', 'Q2', 'Q3'],
                    valueType=int, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1) 
        
        #widget index 7 
        idx += 1 
        box1 = gui.widgetBox(box) 
        gui.comboBox(box1, self, "TRANSITION_SIEGBAHN",
                     label=self.unitLabels()[idx], addSpace=False,
                    items=['KA1_LINE', 'KA2_LINE', 'KB1_LINE', 'KB2_LINE', 'KB3_LINE', 'KB4_LINE', 'KB5_LINE', 'LA1_LINE', 'LA2_LINE', 'LB1_LINE', 'LB2_LINE', 'LB3_LINE', 'LB4_LINE', 'LB5_LINE', 'LB6_LINE', 'LB7_LINE', 'LB9_LINE', 'LB10_LINE', 'LB15_LINE', 'LB17_LINE', 'LG1_LINE', 'LG2_LINE', 'LG3_LINE', 'LG4_LINE', 'LG5_LINE', 'LG6_LINE', 'LG8_LINE', 'LE_LINE', 'LL_LINE', 'LS_LINE', 'LT_LINE', 'LU_LINE', 'LV_LINE'],
                    valueType=int, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1) 
        
        #widget index 8 
        idx += 1 
        box1 = gui.widgetBox(box) 
        gui.comboBox(box1, self, "SHELL",
                     label=self.unitLabels()[idx], addSpace=False,
                    items=['All shells', 'K_SHELL', 'L1_SHELL', 'L2_SHELL', 'L3_SHELL', 'M1_SHELL', 'M2_SHELL', 'M3_SHELL', 'M4_SHELL', 'M5_SHELL', 'N1_SHELL', 'N2_SHELL', 'N3_SHELL', 'N4_SHELL', 'N5_SHELL', 'N6_SHELL', 'N7_SHELL', 'O1_SHELL', 'O2_SHELL', 'O3_SHELL', 'O4_SHELL', 'O5_SHELL', 'O6_SHELL', 'O7_SHELL', 'P1_SHELL', 'P2_SHELL', 'P3_SHELL', 'P4_SHELL', 'P5_SHELL', 'Q1_SHELL', 'Q2_SHELL', 'Q3_SHELL'],
                    valueType=int, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1) 
        
        #widget index 9 
        idx += 1 
        box1 = gui.widgetBox(box) 
        oasysgui.lineEdit(box1, self, "ENERGY",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=float, validator=QDoubleValidator(), orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1) 


        self.process_showers()

        gui.rubber(self.controlArea)

    def unitLabels(self):
         return ["Calculate", "Atomic number", "Element or compound name", "Compound name", "Transition notation", "Transition to (iupac)","Transition from (iupac)","Transition (Siegbahn)","Shell","Energy [keV]"]


    def unitFlags(self):
         return ["True", "self.FUNCTION <= 3 or self.FUNCTION == 6", "self.FUNCTION == 4 or self.FUNCTION == 5 or self.FUNCTION == 7 or self.FUNCTION == 8 or self.FUNCTION == 10", "self.FUNCTION > 100","self.FUNCTION == 0", "(self.FUNCTION == 0 and self.TRANSITION_IUPAC_OR_SIEGBAHN == 0)", "(self.FUNCTION == 0 and self.TRANSITION_IUPAC_OR_SIEGBAHN == 0)", "(self.FUNCTION == 0 and self.TRANSITION_IUPAC_OR_SIEGBAHN == 1)","self.FUNCTION == 1 or self.FUNCTION == 6","self.FUNCTION >= 4 "]


    def compute(self):
        self.setStatusMessage("Running XRAYLIB")

        try:
            sys.stdout = xoppy_util.EmittingStream(textWritten=self.writeStdOut)

            xoppy_calc_xraylib_widget(FUNCTION=self.FUNCTION,ELEMENT=self.ELEMENT,ELEMENTORCOMPOUND=self.ELEMENTORCOMPOUND,COMPOUND=self.COMPOUND,TRANSITION_IUPAC_OR_SIEGBAHN=self.TRANSITION_IUPAC_OR_SIEGBAHN,TRANSITION_IUPAC_TO=self.TRANSITION_IUPAC_TO,TRANSITION_IUPAC_FROM=self.TRANSITION_IUPAC_FROM,TRANSITION_SIEGBAHN=self.TRANSITION_SIEGBAHN,SHELL=self.SHELL,ENERGY=self.ENERGY)

            self.setStatusMessage("")

        except Exception as exception:
            QMessageBox.critical(self, "Error", str(exception), QMessageBox.Ok)

            self.setStatusMessage("Error!")

            raise  exception

    def defaults(self):
         self.resetSettings()

    def help1(self):
        xoppy_util.xoppy_doc('xraylib_widget')

    def writeStdOut(self, text):
        cursor = self.xoppy_output.textCursor()
        cursor.movePosition(QTextCursor.End)
        cursor.insertText(text)
        self.xoppy_output.setTextCursor(cursor)
        self.xoppy_output.ensureCursorVisible()
class ComponentSearchForm():

    data_source = None   
    related_mashups = None
    widget = None
    table = None
    add_btn = None
    graph_form = None
    highlighted_api = None
    highlighted_mashup = None
    categoryWindow = None
    dataSetListWdiget = None
    climateData = None
    
    def show_main_window(self):
        # Get datasets from server
        data = json.load(urllib2.urlopen(RECO_API_SERVER_ADDR + "/getAllDataSets/"))
        # self.climateData = self.loadClimateDataSet(data)
        self.climateData = self.parseServerData(data)

        self.graph_form = matplotlibWidget()
        if self.widget:
            self.widget.destroy()
            self.widget = None;
        self.widget = QWidget()
        self.widget.setMinimumSize(1000, 900)
        self.widget.setWindowTitle("Recommendation PlugIn")
        margin = 30

        # Search Feature
        self.textboxLabel = QLabel(self.widget)
        self.textboxLabel.setText("Describe your goals:")
        self.textboxLabel.move(30, 20)
        self.textboxLabel.show
        self.textbox = QTextEdit(self.widget)
        self.textbox.move(30, 45)
        self.textbox.setFixedWidth(300)
        self.textbox.setFixedHeight(28)

        btn_api = QtGui.QPushButton("Recommend Services", self.widget)
        btn_api.move(30, 80)

        btn_mashup = QtGui.QPushButton("Recommend Workflows", self.widget)
        btn_mashup.move(230, 80)

        btn_api.clicked.connect(self.api_button_clicked)
        btn_mashup.clicked.connect(self.mashups_button_clicked)

        self.table = QTableView(self.widget)
        self.table.clicked.connect(self.table_clicked)
        self.table.setMinimumSize(600, 300)
        self.table.resizeColumnsToContents()
        self.table.move(30, 120)

        # Top Service
        self.textboxLabel = QLabel("Top Datasets (Usage)", self.widget)
        self.textboxLabel.move(650, 20)
        self.textboxLabel.show

        self.listWidget = QListWidget(self.widget)
        topDatasets = json.load(urllib2.urlopen(RECO_API_SERVER_ADDR + "/getTop10UsedDataset/"))
        for topDataset in topDatasets:
            id = int(topDataset["datasetID"])
            item = QListWidgetItem("{} ({})".format(self.climateData[id]["name"], topDataset["usage"]))
            item.setData(Qt.UserRole,self.climateData[id])
            self.listWidget.addItem(item)
        self.listWidget.move(650, 45)
        self.listWidget.resize(280, 380)
        self.listWidget.show()
        self.listWidget.clicked.connect(lambda: self.dataset_clicked(\
            self.listWidget.currentItem().data(Qt.UserRole)))


        # Username input box and button for generating recommendations
        userTopOffset = 430
        self.textboxLabel = QLabel("Enter username for dataset recommendations:", self.widget)
        self.textboxLabel.move(30, userTopOffset)
        self.textboxLabel.show()

        self.userTextbox = QTextEdit(self.widget)
        self.userTextbox.move(30, userTopOffset + 30)
        self.userTextbox.setFixedWidth(200)
        self.userTextbox.setFixedHeight(28)

        userBtn = QPushButton("Recommend Datasets", self.widget)
        userBtn.move(30 + self.userTextbox.width() + 10, userTopOffset + 30)
	#print RECO_API_SERVER_ADDR + "/getTop5RecommendationByUserName/"
	    
	    ## Jan.20 2016 
	    ##  Change the API names here by shenggu 
	    ##  According to github.com/cmusv-sc/RecommendationAPIs
	    ##
        userBtn.clicked.connect(lambda: self.getRecommendations(RECO_API_SERVER_ADDR + "/getTopKContentBasedCFRecommendedDatasetByUsername",
            self.recListFeature))
        userBtn.clicked.connect(lambda: self.getRecommendations(RECO_API_SERVER_ADDR + "/getTopKItemBasedCFRecommendedDatasetByUsername",
            self.recListItem))
        userBtn.clicked.connect(lambda: self.getRecommendations(RECO_API_SERVER_ADDR + "/getTopKUserBasedCFRecommendedDatasetByUsername",
            self.recListUser))
        userBtn.show()

        # Test QlineEdit
        # self.userTextbox2 = QLineEdit(self.widget)
        # self.userTextbox.move(200, userTopOffset + 30)
        # self.userTextbox.setFixedWidth(200)
        # self.userTextbox.setFixedHeight(28)

        # Feature Recommendations
        recTopOffset = 500
        self.textboxLabel = QLabel("Feature Recommendations", self.widget)
        self.textboxLabel.move(30, recTopOffset)
        self.textboxLabel.show()

        self.recListFeature = QListWidget(self.widget)
        self.recListFeature.move(30, recTopOffset + 30)
        self.recListFeature.resize(280, 250)
        self.recListFeature.show()

        # Item-based Recommendations
        self.textboxLabel = QLabel("Item-based Recommendations", self.widget)
        self.textboxLabel.move(340, recTopOffset)
        self.textboxLabel.show

        self.recListItem = QListWidget(self.widget)
        self.recListItem.move(340, recTopOffset + 30)
        self.recListItem.resize(280, 250)
        self.recListItem.show()

        # User-based Recommendations
        self.textboxLabel = QLabel("User-based Recommendations", self.widget)
        self.textboxLabel.move(650, recTopOffset)
        self.textboxLabel.show

        self.recListUser = QListWidget(self.widget)
        self.recListUser.move(650, recTopOffset + 30)
        self.recListUser.resize(280, 250)
        self.recListUser.show()

        # Categories
        categoryTopOffset = 300
        self.textboxLabel = QLabel("Categories", self.widget)
        self.textboxLabel.move(30, recTopOffset + categoryTopOffset)
        self.textboxLabel.show

        button1 = QPushButton("By Agency", self.widget)
        button1.clicked.connect(lambda: self.listCategory_clicked("agency"))
        button1.move(30, recTopOffset + categoryTopOffset + 30)
        button1.show()

        button2 = QPushButton("By Instrument", self.widget)
        button2.clicked.connect(lambda: self.listCategory_clicked("instrument"))
        button2.move(margin + button1.width() + 20, recTopOffset + categoryTopOffset + 30)
        button2.show()

        button3 = QPushButton("By Data Source Input", self.widget)
        button3.clicked.connect(lambda: self.listCategory_clicked("input"))
        button3.move(margin + button1.width() + button2.width() + 40, recTopOffset + categoryTopOffset + 30)
        button3.show()

        # Show and move widget
        self.widget.move(QtGui.QApplication.desktop().screen().rect().center() - \
            self.widget.rect().center())
        self.widget.show()

        # Service and workflow recommendation
        self.add_btn = QPushButton("Add to Palette", self.widget)
        self.add_btn.clicked.connect(self.add_new_api)
        self.add_btn.hide()
        self.add_btn.setFixedWidth(160)
        self.add_btn.move(470, 20)
        
        self.recommendLabel = QLabel("Also Used", self.widget)
        self.recommendLabel.hide()
        self.recommendLabel.setFixedWidth(160)
        self.recommendLabel.move(470, 50)
        
        self.switch_btn_apis = QPushButton("Related Workflows", self.widget)
        self.switch_btn_apis.clicked.connect(self._show_related_apis)
        self.switch_btn_apis.hide()
        self.switch_btn_apis.setFixedWidth(160)
        self.switch_btn_apis.move(470, 80)
        
        self.switch_btn_mashups = QPushButton("Related Modules", self.widget)
        self.switch_btn_mashups.clicked.connect(self._show_related_mashups)
        self.switch_btn_mashups.hide()
        self.switch_btn_mashups.setFixedWidth(160)
        self.switch_btn_mashups.move(470, 80)

    def printMessage(self):
        print "testing"

    def __init__(self, parent=None):
        self.data_source = DataSource()
    
    def getRecommendations(self, url, listWidget):
        # http GET request
        username = str(self.userTextbox.toPlainText())
        url = url + "?username=kzhang&top_num=5" #  + username
        print url
        results = json.load(urllib2.urlopen(url))
        
        # Update recommendation list
        listWidget.clear()
        for result in results:
            dataset = self.climateData[int(result["datasetID"])]
            item = QListWidgetItem(dataset["name"])
            item.setData(Qt.UserRole, dataset)
            listWidget.addItem(item)

        listWidget.clicked.connect(lambda: self.dataset_clicked(\
            listWidget.currentItem().data(Qt.UserRole)))

    def listCategory_clicked(self, category):
        self.categoryWindow = QWidget()
        c = self.categoryWindow
        c.setWindowTitle("Search dataset by category")
        c.setMinimumSize(600, 500)
        
        # Category list showing all posible options
        label1 = QLabel("Options by {0}:".format(category), c)
        label1.move(50, 20)
        label1.show()
        categoryListWidget = QListWidget(c)
        optionSet = set()
        for key in self.climateData:
            optionSet.add(self.climateData[key][category])
        for option in sorted(list(optionSet)):
            item = QListWidgetItem(option)
            item.setData(Qt.UserRole, option)
            categoryListWidget.addItem(item)
        categoryListWidget.move(50, 50)
        categoryListWidget.resize(200, 400)
        categoryListWidget.show()
        categoryListWidget.clicked.connect(lambda: self.categoryItem_clicked(\
            category, categoryListWidget.currentItem().data(Qt.UserRole)))

        # List showing all datasets associated with the selected option
        label2 = QLabel("Available Datasets:", c)
        label2.move(250, 20)
        label2.show()
        self.datasetListWidget = QListWidget(c)
        self.datasetListWidget.move(250, 50)
        self.datasetListWidget.resize(400, 400)
        self.datasetListWidget.show()

        c.move(QtGui.QApplication.desktop().screen().rect().center() - \
            c.rect().center())
        c.show()

    def categoryItem_clicked(self, category, option):
        self.datasetListWidget.clear()
        results = []
        for key in self.climateData:
            if self.climateData[key][category] == option:
                results.append(self.climateData[key])
        for result in sorted(results):
            item = QListWidgetItem(result["name"])
            item.setData(Qt.UserRole,result)
            self.datasetListWidget.addItem(item)
        self.datasetListWidget.clicked.connect(lambda: self.dataset_clicked(\
            self.datasetListWidget.currentItem().data(Qt.UserRole)));


    def dataset_clicked(self, data):
        # Initiate Table
        keyOrder = ["name", "agency", "instrument", "pvar", "var", "units", "grid", \
        "webvar", "input", "start", "end"]
        sortedData = sorted(data.items(), key=lambda dataPair:keyOrder.index(dataPair[0]))
        self.topServiceTable = QTableWidget()
        t = self.topServiceTable
        t.setWindowTitle(data["name"])
        t.resize(550, 400)
        t.setRowCount(len(data.keys()))
        t.setColumnCount(2)
        t.setColumnWidth(0, 100);
        t.setColumnWidth(1, 400);

        # Set label
        t.setHorizontalHeaderLabels(("Variable;Value").split(";"))

        # Set data
        for row, pair in enumerate(sortedData):
            t.setItem(row, 0, QTableWidgetItem(pair[0]))
            t.setItem(row, 1, QTableWidgetItem(pair[1]))

        t.move(QtGui.QApplication.desktop().screen().rect().topRight() - t.rect().topRight())
        t.show()


    def table_clicked(self):
        """
        Click the table, the graph form may change according to the selection.
        """
        model = self.table.selectionModel()
        indexes = model.selectedIndexes()
        for index in indexes:
            row = index.row()
            #            data = index.model().headerData(0,Qt.Horizontal).toString()
            data = index.model().headerData(0,Qt.Horizontal)
            newIndex = index.model().index(row, 0)
            if data == "API":
                api_id = get_api_full_name(newIndex.model().data(newIndex))
                api = self.data_source.api_by_id(api_id)
                print api
                mashups = self.data_source.mashups_by_api(api)
                apis = []
                for mashup in mashups:
                    apis.extend(self.data_source.apis_by_mashup(mashup))
                self.graph_form.draw_apis(apis, api, self.highlighted_api)
            else:
                mashup_id = get_mashup_full_name(newIndex.model().data(newIndex))
                mashup = self.data_source.mashup_by_id(mashup_id)
                if not mashup:
                    return
                apis = self.data_source.apis_by_mashup(mashup)
                mashups = []
                if len(apis) > 0:
                    mashups.extend(self.data_source.mashups_by_api(apis[0]))
                self.graph_form.draw_mashups(mashups, mashup, self.highlighted_mashup)
            return


    def _show_apis(self, apis, recommend=False):
        self.switch_btn_apis.hide()
        self.switch_btn_mashups.hide()
        self.recommendLabel.hide()

        row = 0
        model = QStandardItemModel(len(apis), 4)
        model.setColumnCount(4)
        #QVariant(...) -> ...
        for api in apis:
            model.setData(model.index(row, 0), get_api_name(api))
            model.setData(model.index(row, 1), api['protocols'])
            model.setData(model.index(row, 2), api['provider'])
            model.setData(model.index(row, 3), api['version'])
            row += 1

        model.setHeaderData(0, Qt.Horizontal, "Service") # Replaced "Module" with "Service"
        model.setHeaderData(1, Qt.Horizontal, "Protocol")
        model.setHeaderData(2, Qt.Horizontal, "Provider")
        model.setHeaderData(3, Qt.Horizontal, "Version")

        self.table.setModel(model)
        self.table.resizeColumnsToContents()
        
        if recommend:
            self.recommendLabel.show()
        
        self.add_btn.show()

    def _show_mashups(self, mashups):
        self.switch_btn_apis.hide()
        self.switch_btn_mashups.hide()
        self.recommendLabel.hide()
        row = 0
        model = QStandardItemModel(len(mashups), 4)
        model.setColumnCount(4)
        for mashup in mashups:
            model.setData(model.index(row, 0), get_mashup_name(mashup))
            model.setData(model.index(row, 1), mashup['title'])
            model.setData(model.index(row, 2), mashup['self'])
            model.setData(model.index(row, 3), mashup['description'])
            
            row += 1
        
        model.setHeaderData(0, Qt.Horizontal, "Workflow")
        model.setHeaderData(1, Qt.Horizontal, "Short Description")
        model.setHeaderData(2, Qt.Horizontal, "Provider")
        model.setHeaderData(3, Qt.Horizontal, "Detailed Info")

        self.table.setModel(model)
        self.table.resizeColumnsToContents()
        self.add_btn.show()

    def api_button_clicked(self):
        """
        Trigger to search APIs
        """        
        self.graph_form.draw_api()
        self.graph_form.show()

        # Get user input from textbox
        apis = self.data_source.apis()
        key = str(self.textbox.toPlainText())
        if key:
            self.api_search_button_clicked()
        else:
            self._show_apis(apis)

    def mashups_button_clicked(self):
        """
        Trigger to search mashups
        """
        self.graph_form.draw_mashup()
        self.graph_form.show()

        key = str(self.textbox.toPlainText())
        print key
        print "button clicked"
        if key:
            self.mashup_search_button_clicked()
        else:
            self._show_mashups(self.data_source.mashups())

    #Should probably refactor this into one method.
    def api_search_button_clicked(self):
        """
        Search when keyword is present
        """
        self.highlighted_api = None
        self.highlighted_mashup = None
        key = str(self.textbox.toPlainText())
        if key != "":
            apis = self.data_source.search_api(key)
            self._show_apis(apis)

    def mashup_search_button_clicked(self):
        """
        Search when keyword is present
        """
        self.highlighted_api = None
        self.highlighted_mashup = None
        key = str(self.textbox.toPlainText())
        if key != "":
            mashups = self.data_source.search_mashup(key)
            self._show_mashups(mashups)
    
    def add_new_api(self):
        """
        Add new api to the modules package.
        """
        apis = self.data_source.apis()
        model = self.table.selectionModel()
        indexes = model.selectedIndexes()
        for index in indexes:
            api = apis[index.row()]
            self._add_new_api(api)
            return
    
    def add_related_api(self):
        objs = []
        for mashup in self.related_mashups:
            objs.append(mashup)
            for api in mashup["related_mashups"]:
                objs.append(api)
        model = self.table.selectionModel()
        indexes = model.selectedIndexes()
        for index in indexes:
            api = objs[index.row()]
            if api.get("protocols"):
                self._add_new_api(api)
                return

    def _show_related_mashups(self):
        self.switch_btn_apis.hide()
        self.switch_btn_mashups.hide()
        self.recommendLabel.hide()
        
        apis = []
        objs = []
        for mashup in self.related_mashups:
            apis.extend(mashup["related_mashups"])

        for api in apis:
            objs.append(api)
            mashups = self.data_source.mashups_by_api(api)
            objs.extend(mashups)

        row = 0
        model = QStandardItemModel(len(objs), 4)
        model.setColumnCount(4)
        for obj in objs:
            if obj.get('protocols'):
                model.setData(model.index(row, 0), get_api_name(obj))
                model.setData(model.index(row, 1), obj['protocols'])
                model.setData(model.index(row, 2), obj['provider'])
            else:
                model.setData(model.index(row, 3), get_mashup_name(obj))
            row += 1

        model.setHeaderData(0, Qt.Horizontal, "API")
        model.setHeaderData(1, Qt.Horizontal, "Protocols")
        model.setHeaderData(2, Qt.Horizontal, "Provider")
        model.setHeaderData(3, Qt.Horizontal, "Mashup")
        self.table.setModel(model)
        self.table.resizeColumnsToContents()
        self.switch_btn_apis.show()

    def _show_related_apis(self):
        self.switch_btn_apis.hide()
        self.switch_btn_mashups.hide()
        self.recommendLabel.hide()
        
        row = 0
        objs = []
        for mashup in self.related_mashups:
            objs.append(mashup)
            for api in mashup["related_mashups"]:
                objs.append(api)
        #Combining similarity and related.
        similar_apis = self.data_source.search_api_similarity(self.highlighted_api)
        #return str(mashup['id'])[(len("http://www.programmableweb.com/mashup/")):]
        objs.append({'id': "http://www.programmableweb.com/mashup/Using-Similarity-Metric"})
        objs.extend(similar_apis)
        #Combining similarity and related.

        #http://localhost:9000/getReputation/John%20Lions
        model = QStandardItemModel(len(objs), 6)
        for obj in objs:
            if obj.get('protocols'):
                model.setData(model.index(row, 1), get_api_name(obj))
                model.setData(model.index(row, 2), obj['protocols'])
                model.setData(model.index(row, 3), obj['provider'])
                model.setData(model.index(row, 4), obj['version'])
                #trust  = requests.get('http://localhost:9000/getReputation/Luis Ramos').content
                model.setData(model.index(row, 5), str(random.random()))
                #model.setData(model.index(row, 5), QVariant(trust))
            else:
                model.setData(model.index(row, 0), get_mashup_name(obj))
            row += 1

        model.setHeaderData(0, Qt.Horizontal, "Mashup")
        model.setHeaderData(1, Qt.Horizontal, "API")
        model.setHeaderData(2, Qt.Horizontal, "Protocols")
        model.setHeaderData(3, Qt.Horizontal, "Provider")
        model.setHeaderData(4, Qt.Horizontal, "Version")
        model.setHeaderData(5, Qt.Horizontal, "Trust")

        self.table.setModel(model)
        self.table.resizeColumnsToContents()
        self.switch_btn_mashups.show()

    def _add_new_api(self, api):
        self.highlighted_api = api
        mashups = self.data_source.mashups_by_api(api)
        for mashup in mashups:
            mashup['related_mashups'] = (self.data_source.apis_by_mashup(mashup))
        if len(mashups) > 0:
            self.related_mashups = mashups
            self._show_related_apis()
        manager = core.packagemanager.get_package_manager()
        reg = core.modules.module_registry.get_module_registry()
        package = manager.get_package("edu.cmu.sv.components", "1.0.0")
        core.modules.module_registry.set_current_package(package)

        if (api["protocols"] == "SOAP" and api["wsdl"] != ""):
            s = Service(api["wsdl"])
#            add_new_service(s, api["wsdl"])
        else:
            new_module = vistrails_module.new_module(Module, get_api_name(api))
            reg.add_module(new_module)
    
            reg.add_input_port(new_module, "value1",
                         (core.modules.basic_modules.String, 'the first argument'))
            reg.add_input_port(new_module, "value2",
                         (core.modules.basic_modules.String, 'the second argument'))
            reg.add_output_port(new_module, "value",
                          (core.modules.basic_modules.String, 'the result'))

    # def loadClimateDataSet(self):
    #     filename = '/home/hywel/Documents/VisTrailsRecommendation/vistrails_current/vistrails/packages/componentSearch/Climate_Datasets.csv'
    #     with open(filename, mode='r') as infile:
    #         reader = csv.reader(infile)
    #         next(reader, None)
    #         with open('coors_new.csv', mode='w') as outfile:
    #             writer = csv.writer(outfile)
    #             climateData = {}
    #             for row in reader:
    #                 id = int(row[0])
    #                 climateData[id] = {}
    #                 climateData[id]["dataset"] = row[1]
    #                 climateData[id]["agency"] = row[2]
    #                 climateData[id]["instrument"] = row[3]
    #                 climateData[id]["pvar"] = row[4]
    #                 climateData[id]["var"] = row[5]
    #                 climateData[id]["units"] = row[6]
    #                 climateData[id]["grid"] = row[7]
    #                 climateData[id]["webvar"] = row[8]
    #                 climateData[id]["input"] = row[9]
    #                 climateData[id]["start"] = row[10]
    #                 climateData[id]["end"] = row[11]
    #     return climateData

    def parseServerData(self, data):
        climateData = {}
        for dataset in data:
            id = int(dataset["datasetID"])
            climateData[id] = {}
            climateData[id]["name"] = dataset["dataset"]
            climateData[id]["agency"] = dataset["agency"]
            climateData[id]["instrument"] = dataset["instrument"]
            climateData[id]["pvar"] = dataset["pvar"]
            climateData[id]["var"] = dataset["var"]
            climateData[id]["units"] = dataset["units"]
            climateData[id]["grid"] = dataset["grid"]
            climateData[id]["webvar"] = dataset["webvar"]
            climateData[id]["input"] = dataset["input"]
            climateData[id]["start"] = dataset["start"]
            climateData[id]["end"] = dataset["end"]
        return climateData
class CMACForm():

    data_source = None
    
    related_mashups = None
    
    widget = None
    
    table = None
    
    add_btn = None
    
    graph_form = None
    
    highlighted_api = None
    
    highlighted_mashup = None

    #TODO: Make global config
    SERVICE_URL = "http://einstein.sv.cmu.edu:9002/svc/twoDimMap?model={0}&var={1}&start_time={2}&end_time={3}&lon1={4}&lon2={5}&lat1={6}&lat2={7}&months={8}&scale={9}"
    
    def show_main_window(self):
        self.graph_form = matplotlibWidget()
        if self.widget:
            self.widget.destroy()
            self.widget = None;
        self.widget = QWidget()
        self.widget.setMinimumSize(800, 600)

        ds_label = QLabel(self.widget)
        ds_label.setText("Data Source:")
        ds_label.move(30,20)
        self.ds = QtGui.QComboBox(self.widget)
        self.ds.addItem("NASA/MODIS")
        self.ds.addItem("NASA/AMSRE")
        self.ds.move(30, 40)

        var_label = QLabel(self.widget)
        var_label.setText("Variable Name:")
        var_label.move(150,20)
        self.var = QtGui.QComboBox(self.widget)
        self.var.addItem("Precipitation Flux")
        self.var.addItem("Total Cloud Fraction")
        self.var.move(150, 40)
        self.var.setCurrentIndex(1)
        
        self.sdate = QTextEdit("2004-01", self.widget)
        self.sdate.move(30, 100)
        self.sdate.setFixedWidth(100)
        self.sdate.setFixedHeight(30)
#        print self.sdate.toPlainText()

        sdate_label = QLabel(self.widget)
        sdate_label.setText("Start Date")
        sdate_label.move(30, 80)

        self.edate = QTextEdit("2004-12", self.widget)
        self.edate.move(150, 100)
        self.edate.setFixedWidth(100)
        self.edate.setFixedHeight(30)

        edate_label = QLabel(self.widget)
        edate_label.setText("End Date")
        edate_label.move(150, 80)

        self.slat = QTextEdit("-90", self.widget)
        self.slat.move(270, 100)
        self.slat.setFixedWidth(100)
        self.slat.setFixedHeight(30)

        slat_label = QLabel(self.widget)
        slat_label.setText("Start Lat (deg)")
        slat_label.move(270, 80)

        self.elat = QTextEdit("90", self.widget)
        self.elat.move(390, 100)
        self.elat.setFixedWidth(100)
        self.elat.setFixedHeight(30)

        elat_label = QLabel(self.widget)
        elat_label.setText("End Lat (deg)")
        elat_label.move(390, 80)

        self.slon = QTextEdit("0", self.widget)
        self.slon.move(510, 100)
        self.slon.setFixedWidth(100)
        self.slon.setFixedHeight(30)

        slon_label = QLabel(self.widget)
        slon_label.setText("Start Lon (deg)")
        slon_label.move(510, 80)

        self.elon = QTextEdit("360", self.widget)
        self.elon.move(630, 100)
        self.elon.setFixedWidth(100)
        self.elon.setFixedHeight(30)

        elon_label = QLabel(self.widget)
        elon_label.setText("End Lon (deg)")
        elon_label.move(630, 80)

        self.cb1 = QtGui.QCheckBox('January', self.widget)
        self.cb1.move(30,160)
        self.cb1.setChecked(True)
        self.cb2 = QtGui.QCheckBox('February', self.widget)
        self.cb2.move(130,160)
        self.cb2.setChecked(True)
        self.cb3 = QtGui.QCheckBox('March', self.widget)
        self.cb3.move(230,160)
        self.cb3.setChecked(True)
        self.cb4 = QtGui.QCheckBox('April', self.widget)
        self.cb4.move(330,160)
        self.cb4.setChecked(True)
        self.cb5 = QtGui.QCheckBox('May', self.widget)
        self.cb5.move(430,160)
        self.cb5.setChecked(True)
        self.cb6 = QtGui.QCheckBox('June', self.widget)
        self.cb6.move(530,160)
        self.cb6.setChecked(True)
        self.cb7 = QtGui.QCheckBox('July', self.widget)
        self.cb7.move(30,200)
        self.cb7.setChecked(True)
        self.cb8 = QtGui.QCheckBox('August', self.widget)
        self.cb8.move(130,200)
        self.cb8.setChecked(True)
        self.cb9 = QtGui.QCheckBox('September', self.widget)
        self.cb9.move(230,200)
        self.cb9.setChecked(True)
        self.cb10 = QtGui.QCheckBox('October', self.widget)
        self.cb10.move(330,200)
        self.cb10.setChecked(True)
        self.cb11 = QtGui.QCheckBox('November', self.widget)
        self.cb11.move(430,200)
        self.cb11.setChecked(True)
        self.cb12 = QtGui.QCheckBox('December', self.widget)
        self.cb12.move(530,200)
        self.cb12.setChecked(True)

        cscale_label = QLabel(self.widget)
        cscale_label.setText("Color Scale")
        cscale_label.move(30, 240)
        self.rbg = QtGui.QButtonGroup(self.widget)
        self.rb1 = QtGui.QRadioButton('Linear', self.widget)
        self.rb1.move(130,240)

        self.rb2 = QtGui.QRadioButton('Logarithmic', self.widget)
        self.rb2.move(230,240)
        self.rbg.addButton(self.rb1)
        self.rbg.addButton(self.rb2)
        self.rb1.setChecked(True)
#        print self.rb1.isChecked()
        self.btn_call_service = QtGui.QPushButton("Call Service", self.widget)
        self.btn_call_service.move(30,280)
        self.btn_call_service.clicked.connect(self.button_call_service)


        btn_api = QtGui.QPushButton("Recommend Modules", self.widget)
        btn_api.move(30, 20)
        btn_api.hide()

        btn_mashup = QtGui.QPushButton("Recommend Workflows", self.widget)
        btn_mashup.move(200, 20)
        btn_mashup.hide()
        self.widget.show()

    def __init__(self, parent=None):
        self.data_source = DataSource()
        
    def table_clicked(self):
        """
        Click the table, the graph form may change according to the selection.
        """
        model = self.table.selectionModel()
        indexes = model.selectedIndexes()
        for index in indexes:
            row = index.row()
            data = index.model().headerData(0,Qt.Horizontal).toString()
            newIndex = index.model().index(row, 0)
            if data == "API":
                api_id = get_api_full_name(newIndex.model().data(newIndex).toString())
                api = self.data_source.api_by_id(api_id)
                print api
                mashups = self.data_source.mashups_by_api(api)
                apis = []
                for mashup in mashups:
                    apis.extend(self.data_source.apis_by_mashup(mashup))
                self.graph_form.draw_apis(apis, api, self.highlighted_api)
            else:
                mashup_id = get_mashup_full_name(newIndex.model().data(newIndex).toString())
                mashup = self.data_source.mashup_by_id(mashup_id)
                if not mashup:
                    return
                apis = self.data_source.apis_by_mashup(mashup)
                mashups = []
                if len(apis) > 0:
                    mashups.extend(self.data_source.mashups_by_api(apis[0]))
                self.graph_form.draw_mashups(mashups, mashup, self.highlighted_mashup)
            return


    def _show_apis(self, apis, recommend=False):
        self.switch_btn_apis.hide()
        self.switch_btn_mashups.hide()
        self.recommendLabel.hide()

        row = 0
        model = QStandardItemModel(len(apis), 4)
        model.setColumnCount(4)
        for api in apis:
            model.setData(model.index(row, 0), QVariant(get_api_name(api)))
            model.setData(model.index(row, 1), QVariant(api['protocols']))
            model.setData(model.index(row, 2), QVariant(api['provider']))
            model.setData(model.index(row, 3), QVariant(api['version']))
            row += 1

        model.setHeaderData(0, Qt.Horizontal, QVariant("Module"))
        model.setHeaderData(1, Qt.Horizontal, QVariant("Protocol"))
        model.setHeaderData(2, Qt.Horizontal, QVariant("Provider"))
        model.setHeaderData(3, Qt.Horizontal, QVariant("Version"))

#        model.setHeaderData(0, Qt.Horizontal, QVariant("API"))
#        model.setHeaderData(1, Qt.Horizontal, QVariant("Protocols"))
#        model.setHeaderData(2, Qt.Horizontal, QVariant("Provider"))
#        model.setHeaderData(3, Qt.Horizontal, QVariant("Version"))

        self.table.setModel(model)
        self.table.resizeColumnsToContents()
        
        if recommend:
            self.recommendLabel.show()
        
        self.add_btn.show()

    def _show_mashups(self, mashups):
        self.switch_btn_apis.hide()
        self.switch_btn_mashups.hide()
        self.recommendLabel.hide()
        row = 0
        model = QStandardItemModel(len(mashups), 4)
        model.setColumnCount(4)
        for mashup in mashups:
            model.setData(model.index(row, 0), QVariant(get_mashup_name(mashup)))
            model.setData(model.index(row, 1), QVariant(mashup['title']))
            model.setData(model.index(row, 2), QVariant(mashup['self']))
            model.setData(model.index(row, 3), QVariant(mashup['description']))
            
            row += 1
        
        model.setHeaderData(0, Qt.Horizontal, QVariant("Workflow"))
        model.setHeaderData(1, Qt.Horizontal, QVariant("Short Description"))
        model.setHeaderData(2, Qt.Horizontal, QVariant("Provider"))
        model.setHeaderData(3, Qt.Horizontal, QVariant("Detailed Info"))

#        model.setHeaderData(0, Qt.Horizontal, QVariant("Info"))
#        model.setHeaderData(1, Qt.Horizontal, QVariant("Title"))
#        model.setHeaderData(2, Qt.Horizontal, QVariant("self"))
#        model.setHeaderData(3, Qt.Horizontal, QVariant("Description"))

        self.table.setModel(model)
        self.table.resizeColumnsToContents()
        self.add_btn.show()


    def button_call_service(self):
        """
        Trigger to call JPL's service
        """
        var_dict = {"Total Cloud Fraction" : "clt",
                        "Surface Temperature" : "ts",
                        "Sea Surface Temperature" : "tos",
                        "Precipitation Flux" : "pr",
                        "Eastward Near-Surface Wind" : "uas",
                        "Northward Near-Surface Wind" : "vas",
                        "Near-Surface Wind Speed" : "sfcWind",
                        "Sea Surface Height" : "zos",
                        "Leaf Area Index" : "lai",
                        "Equivalent Water Height Over Land" : "zl",
                        "Equivalent Water Height Over Ocean" : "zo",
                        "Ocean Heat Content Anomaly within 700 m Depth" : "ohc700",
                        "Ocean Heat Content Anomaly within 2000 m Depth" : "ohc2000",
                        "Surface Downwelling Longwave Radiation" : "rlds",
                        "Surface Downwelling Shortwave Radiation" : "rlus",
                        "Surface Upwelling Longwave Radiation" : "rsds",
                        "Surface Upwelling Shortwave Radiation" : "rsus",
                        "Surface Downwelling Clear-Sky Longwave Radiation" : "rldscs",
                        "Surface Downwelling Clear-Sky Shortwave Radiation" : "rsdscs",
                        "Surface Upwelling Clear-Sky Shortwave Radiation" : "rsuscs",
                        "TOA Incident Shortwave Radiation" : "rsdt",
                        "TOA Outgoing Clear-Sky Longwave Radiation" : "rlutcs",
                        "TOA Outgoing Longwave Radiation" : "rlut",
                        "TOA Outgoing Clear-Sky Shortwave Radiation" : "rsutcs",
                        "TOA Outgoing Shortwave Radiation" : "rsut"}
        a1 = re.sub('/', '_', str(self.ds.currentText()))
        a2 = var_dict[str(self.var.currentText())]
        a3 = re.sub('-', '', str(self.sdate.toPlainText()))
        a4 = re.sub('-', '',str(self.edate.toPlainText()))
        a5 = self.slon.toPlainText()
        a6 = self.elon.toPlainText()
        a7 = self.slat.toPlainText()
        a8 = self.elat.toPlainText()
        months = [self.cb1, self.cb2, self.cb3, self.cb4, self.cb5, self.cb6, self.cb7, self.cb8, self.cb9, self.cb10, self.cb11, self.cb12]
        months = [x.isChecked() for x in months]
        a9 = []
        for i in range(0,12):
            if months[i]:
                a9.append(str(i+1))
        a9 = ",".join(a9)
        a10 = 0 if self.rb1.isChecked() else 4
        r = requests.get(self.SERVICE_URL.format(a1,a2,a3,a4,a5,a6,a7,a8,a9, a10))
        print r.text
        

        
    def api_button_clicked(self):
        """
        Trigger to search APIs
        """        
        self.graph_form.draw_api()
        self.graph_form.show()

        apis = self.data_source.apis()
        key = str(self.textbox.toPlainText())
        #Has key or not has key, it is different.
        if key:
            self.api_search_button_clicked()
        else:
            self._show_apis(apis)

    def mashups_button_clicked(self):
        """
        Trigger to search mashups
        """
        self.graph_form.draw_mashup()
        self.graph_form.show()

        key = str(self.textbox.toPlainText())
        print key
        print "button clicked"
        if key:
            self.mashup_search_button_clicked()
        else:
            self._show_mashups(self.data_source.mashups())

    #Should probably refactor this into one method.
    def api_search_button_clicked(self):
        """
        Search when no keyword
        """
        self.highlighted_api = None
        self.highlighted_mashup = None
        key = str(self.textbox.toPlainText())
        if key != "":
            apis = self.data_source.search_api(key)
            self._show_apis(apis)

    def mashup_search_button_clicked(self):
        """
        Search when no keyword
        """
        self.highlighted_api = None
        self.highlighted_mashup = None
        key = str(self.textbox.toPlainText())
        if key != "":
            mashups = self.data_source.search_mashup(key)
            self._show_mashups(mashups)
    
    def add_new_api(self):
        """
        Add new api to the modules package.
        """
        apis = self.data_source.apis()
        model = self.table.selectionModel()
        indexes = model.selectedIndexes()
        for index in indexes:
            api = apis[index.row()]
            self._add_new_api(api)
            return
    
    def add_related_api(self):
        objs = []
        for mashup in self.related_mashups:
            objs.append(mashup)
            for api in mashup["related_mashups"]:
                objs.append(api)
        model = self.table.selectionModel()
        indexes = model.selectedIndexes()
        for index in indexes:
            api = objs[index.row()]
            if api.get("protocols"):
                self._add_new_api(api)
                return

    def _show_related_mashups(self):
        self.switch_btn_apis.hide()
        self.switch_btn_mashups.hide()
        self.recommendLabel.hide()
        
        apis = []
        objs = []
        for mashup in self.related_mashups:
            apis.extend(mashup["related_mashups"])

        for api in apis:
            objs.append(api)
            mashups = self.data_source.mashups_by_api(api)
            objs.extend(mashups)

        row = 0
        model = QStandardItemModel(len(objs), 4)
        model.setColumnCount(4)
        for obj in objs:
            if obj.get('protocols'):
                model.setData(model.index(row, 0), QVariant(get_api_name(obj)))
                model.setData(model.index(row, 1), QVariant(obj['protocols']))
                model.setData(model.index(row, 2), QVariant(obj['provider']))
            else:
                model.setData(model.index(row, 3), QVariant(get_mashup_name(obj)))
            row += 1

        model.setHeaderData(0, Qt.Horizontal, QVariant("API"))
        model.setHeaderData(1, Qt.Horizontal, QVariant("Protocols"))
        model.setHeaderData(2, Qt.Horizontal, QVariant("Provider"))
        model.setHeaderData(3, Qt.Horizontal, QVariant("Mashup"))
        self.table.setModel(model)
        self.table.resizeColumnsToContents()
        self.switch_btn_apis.show()

    def _show_related_apis(self):
        self.switch_btn_apis.hide()
        self.switch_btn_mashups.hide()
        self.recommendLabel.hide()
        
        row = 0
        objs = []
        for mashup in self.related_mashups:
            objs.append(mashup)
            for api in mashup["related_mashups"]:
                objs.append(api)
        #Combining similarity and related.
        similar_apis = self.data_source.search_api_similarity(self.highlighted_api)
        #return str(mashup['id'])[(len("http://www.programmableweb.com/mashup/")):]
        objs.append({'id': "http://www.programmableweb.com/mashup/Using-Similarity-Metric"})
        objs.extend(similar_apis)
        #Combining similarity and related.

        #http://localhost:9000/getReputation/John%20Lions
        model = QStandardItemModel(len(objs), 6)
        for obj in objs:
            if obj.get('protocols'):
                model.setData(model.index(row, 1), QVariant(get_api_name(obj)))
                model.setData(model.index(row, 2), QVariant(obj['protocols']))
                model.setData(model.index(row, 3), QVariant(obj['provider']))
                model.setData(model.index(row, 4), QVariant(obj['version']))
                trust  = requests.get('http://localhost:9000/getReputation/Luis Ramos').content
                #model.setData(model.index(row, 5), QVariant(str(random.random())))
                model.setData(model.index(row, 5), QVariant(trust))
            else:
                model.setData(model.index(row, 0), QVariant(get_mashup_name(obj)))
            row += 1

        model.setHeaderData(0, Qt.Horizontal, QVariant("Mashup"))
        model.setHeaderData(1, Qt.Horizontal, QVariant("API"))
        model.setHeaderData(2, Qt.Horizontal, QVariant("Protocols"))
        model.setHeaderData(3, Qt.Horizontal, QVariant("Provider"))
        model.setHeaderData(4, Qt.Horizontal, QVariant("Version"))
        model.setHeaderData(5, Qt.Horizontal, QVariant("Trust"))

        self.table.setModel(model)
        self.table.resizeColumnsToContents()
        self.switch_btn_mashups.show()

    def _add_new_api(self, api):
        self.highlighted_api = api
        mashups = self.data_source.mashups_by_api(api)
        for mashup in mashups:
            mashup['related_mashups'] = (self.data_source.apis_by_mashup(mashup))
        if len(mashups) > 0:
            self.related_mashups = mashups
            self._show_related_apis()
        manager = core.packagemanager.get_package_manager()
        reg = core.modules.module_registry.get_module_registry()
        package = manager.get_package("edu.cmu.sv.components", "1.0.0")
        core.modules.module_registry.set_current_package(package)

        if (api["protocols"] == "SOAP" and api["wsdl"] != ""):
            s = Service(api["wsdl"])
            #pass?
#            add_new_service(s, api["wsdl"])
        else:
            new_module = vistrails_module.new_module(Module, get_api_name(api))
            reg.add_module(new_module)
    
            reg.add_input_port(new_module, "value1",
                         (core.modules.basic_modules.String, 'the first argument'))
            reg.add_input_port(new_module, "value2",
                         (core.modules.basic_modules.String, 'the second argument'))
            reg.add_output_port(new_module, "value",
                          (core.modules.basic_modules.String, 'the result'))
Example #5
0
class Ui_RightWidget(object):
    def setupUi(self, RightWidget, server):
        RightWidget.setMinimumHeight(500)

        main_layout = QVBoxLayout(RightWidget)
        main_layout.setContentsMargins(0, 0, 0, 0)
        main_layout.setSpacing(10)

        # We hide all the main elements, to allow the proper viewer to enable
        # (only) the elements that uses.

        if hasattr(server, 'wild_chars'):
            self.text_map = QTextEdit()
            self.text_map.setObjectName('text_map')
            self.text_map.setVisible(False)
            self.text_map.setFocusPolicy(Qt.NoFocus)
            self.text_map.setAutoFillBackground(True)
            self.text_map.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
            self.text_map.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
            self.text_map.setUndoRedoEnabled(False)
            self.text_map.setReadOnly(True)
            # for some unknown reason, the fontmetrics of the text map is wrong
            # if the font is set with a global stylesheet, so we set it on the
            # specific widget.
            self.text_map.setStyleSheet("QTextEdit { font: 13px \"Courier\";}")
            main_layout.addWidget(self.text_map)

            # We calculate the map area size using the size of the font used. We
            # assume that the font is a monospace ones.
            font_metrics = self.text_map.fontMetrics()
            self.text_map.setFixedWidth(font_metrics.width('#' * server.map_width))
            self.text_map.setFixedHeight(font_metrics.height() * server.map_height)

            # The rightwidget width is determined by the map area size
            RightWidget.setMinimumWidth(self.text_map.width())
        else:
            RightWidget.setMinimumWidth(220)

        self.box_status = QWidget()
        self.box_status.setVisible(False)
        main_layout.addWidget(self.box_status)

        status_layout = QGridLayout(self.box_status)
        status_layout.setContentsMargins(5, 5, 5, 0)
        status_layout.setHorizontalSpacing(0)
        status_layout.setVerticalSpacing(15)

        label_health = QLabel()
        label_health.setMinimumWidth(80)
        label_health.setText(QApplication.translate("RightWidget", "Health"))
        status_layout.addWidget(label_health, 0, 0)

        self.bar_health = QProgressBar()
        self.bar_health.setObjectName("bar_health")
        self.bar_health.setFixedHeight(22)
        self.bar_health.setProperty("value", QVariant(100))
        self.bar_health.setTextVisible(False)
        status_layout.addWidget(self.bar_health, 0, 1)

        label_mana = QLabel()
        label_mana.setMinimumWidth(80)
        label_mana.setText(QApplication.translate("RightWidget", "Mana"))
        status_layout.addWidget(label_mana, 1, 0)

        self.bar_mana = QProgressBar()
        self.bar_mana.setObjectName("bar_mana")
        self.bar_mana.setFixedHeight(22)
        self.bar_mana.setProperty("value", QVariant(100))
        self.bar_mana.setTextVisible(False)
        status_layout.addWidget(self.bar_mana, 1, 1)

        label_movement = QLabel()
        label_movement.setMinimumWidth(80)
        label_movement.setText(QApplication.translate("RightWidget", "Movement"))
        status_layout.addWidget(label_movement, 2, 0)

        self.bar_movement = QProgressBar()
        self.bar_movement.setObjectName("bar_movement")
        self.bar_movement.setFixedHeight(22)
        self.bar_movement.setProperty("value", QVariant(100))
        self.bar_movement.setTextVisible(False)
        status_layout.addWidget(self.bar_movement, 2, 1)

        main_layout.addStretch()