from Orange.data import Table
from Orange.classification import KNNLearner, SklModel
from Orange.widgets import widget, gui
from Orange.widgets.settings import Setting
from Orange.widgets.utils.owlearnerwidget import OWProvidesLearner
from Orange.widgets.utils.sql import check_sql_input


class OWKNNLearner(OWProvidesLearner, widget.OWWidget):
    name = "Nearest Neighbors"
    description = "k-nearest neighbors classification algorithm."
    icon = "icons/KNN.svg"
    inputs = [("Data", Table, "set_data")] + OWProvidesLearner.inputs
    outputs = [("Learner", KNNLearner), ("Classifier", SklModel)]

    want_main_area = False
    resizing_enabled = False

    learner_name = Setting("kNN")
    n_neighbors = Setting(5)
    metric_index = Setting(0)
    weight_type = Setting(0)

    def __init__(self):
        super().__init__()
        self.data = None
        self.preprocessors = None

        box = gui.widgetBox(self.controlArea, "Learner/Classifier Name")
        gui.lineEdit(box, self, "learner_name")

        box = gui.widgetBox(self.controlArea, "Neighbors")
        gui.spin(box, self, "n_neighbors", 1, 100, label="Number of neighbors")

        box = gui.widgetBox(box, "Metric")
        box.setFlat(True)

        gui.comboBox(box, self, "metric_index",
                     items=["Euclidean", "Manhattan", "Maximal", "Mahalanobis"])
        self.metrics = ["euclidean", "manhattan", "chebyshev", "mahalanobis"]

        self.weights = ["uniform", "distance"]
        gui.comboBox(self.controlArea, self, "weight_type",
		     box='Neigbours weight',			
                     items=[i.capitalize() for i in self.weights])

        gui.button(self.controlArea, self, "Apply",
                   callback=self.apply, default=True)
        self.controlArea.layout().addWidget(self.report_button)

        self.apply()

    @check_sql_input
    def set_data(self, data):
        self.data = data
        if data is not None:
            self.apply()

    LEARNER = KNNLearner

    def apply(self):
        learner = self.LEARNER(
            n_neighbors=self.n_neighbors,
            metric=self.metrics[self.metric_index],
            weights=self.weights[self.weight_type],
            preprocessors=self.preprocessors
        )
        learner.name = self.learner_name
        classifier = None

        if self.data is not None:
            self.error(0)
            if not learner.check_learner_adequacy(self.data.domain):
                self.error(0, learner.learner_adequacy_err_msg)
            else:
                classifier = learner(self.data)
                classifier.name = self.learner_name

        self.send("Learner", learner)
        self.send("Classifier", classifier)

    def send_report(self):
        self.report_items((("Name", self.learner_name),))
        self.report_items("Model parameters", (
            ("Number of neighbours", self.n_neighbors),
            ("Metric", self.metrics[self.metric_index].capitalize()),
	    ("Weight", self.weights[self.weight_type].capitalize())))
        if self.data:
            self.report_data("Data", self.data)

if __name__ == "__main__":
    import sys
    from PyQt4.QtGui import QApplication

    a = QApplication(sys.argv)
    ow = OWKNNLearner()
    d = Table('iris')
    ow.set_data(d)
    ow.show()
    a.exec_()
    ow.saveSettings()

@@ -1,20 +1,85 @@
+from PyQt4.QtGui import QHBoxLayout%0Afrom PyQt4.QtCore import Qt%0A%0A
 from Orange.data imp
@@ -722,16 +722,124 @@
  False%0A%0A
+    weights = %5B%22uniform%22, %22distance%22%5D%0A    metrics = %5B%22euclidean%22, %22manhattan%22, %22chebyshev%22, %22mahalanobis%22%5D%0A%0A
     lear
@@ -1323,18 +1323,17 @@
 ighbors%22
-)%0A
+,
 %0A       
@@ -1337,69 +1337,41 @@
     
-box = gui.widgetBox(box, %22Metric%22)%0A        box.setFlat(True)%0A
+         alignment=Qt.AlignRight)
 %0A   
@@ -1414,16 +1414,32 @@
 _index%22,
+ label=%22Metric%22,
 %0A       
@@ -1456,193 +1456,100 @@
     
-items=%5B%22Euclidean%22, %22Manhattan%22, %22Maximal%22, %22Mahalanobis%22%5D)%0A        self.metrics = %5B%22euclidean%22, %22manhattan%22, %22chebyshev%22, %22mahalanobis%22%5D%0A%0A        self.weights = %5B%22uniform%22, %22distance%22%5D
+orientation=%22horizontal%22,%0A                     items=%5Bi.capitalize() for i in self.metrics%5D)
 %0A   
@@ -1566,32 +1566,19 @@
 omboBox(
-self.controlArea
+box
 , self, 
@@ -1595,42 +1595,71 @@
 pe%22,
-%0A%09%09     box='Neigbours weight',%09%09%09
+ label='Weight',%0A                     orientation=%22horizontal%22,
 %0A   
@@ -1736,34 +1736,103 @@
    g
-ui.button(self.controlArea
+ = QHBoxLayout()%0A        self.controlArea.layout().addLayout(g)%0A        apply = gui.button(None
 , se
@@ -1844,16 +1844,24 @@
 Apply%22,%0A
+        
         
@@ -1914,32 +1914,17 @@
         
-self.controlArea
+g
 .layout(
@@ -1955,16 +1955,51 @@
 button)%0A
+        g.layout().addWidget(apply)
 %0A       
@@ -3150,9 +3150,16 @@
 )),%0A
-%09
+        
     
@@ -3287,16 +3287,17 @@
 .data)%0A%0A
+%0A
 if __nam

Improve layout