Index: /trunk/FACT++/gui/FactGui.h
===================================================================
--- /trunk/FACT++/gui/FactGui.h	(revision 10958)
+++ /trunk/FACT++/gui/FactGui.h	(revision 10959)
@@ -278,5 +278,5 @@
     }
 
-    char* GetObjectInfo(Int_t px, Int_t py) const
+    char *GetObjectInfo(Int_t px, Int_t py) const
     {
         static stringstream stream;
@@ -290,5 +290,7 @@
         stream.seekp(0);
         if (idx>=0)
-            stream << "Pixel=" << idx << "(" <</* fMapHW[idx] <<*/ ")   Data=" << fData[idx] << '\0';
+        {
+            stream << "Pixel=" << idx << "   Data=" << fData[idx] << '\0';
+        }
 
         str = stream.str();
@@ -330,6 +332,8 @@
     valarray<int8_t> fFtuStatus;
 
-//    vector<int>  fMapHW; // Software -> Hardware
-//    map<int,int> fMapSW; // Hardware -> Software
+    vector<int>  fPixelMapHW; // Software -> Hardware
+    vector<int>  fPatchMapHW; // Software -> Hardware
+
+    bool fInChoosePatch; // FIXME. Find a better solution
 
     DimStampedInfo fDimDNS;
@@ -1152,8 +1156,13 @@
 
         if (fThresholdIdx->value()>=0)
-            fPatchRate->setValue(sdata.fRatePatch[fThresholdIdx->value()]);
+        {
+            const int isw = fThresholdIdx->value();
+            const int ihw = fPatchMapHW[isw];
+            fPatchRate->setValue(sdata.fRatePatch[ihw]);
+        }
 
         valarray<double> dat(0., 1440);
 
+        // fPatch converts from software id to software patch id
         for (int i=0; i<1440; i++)
             dat[i] = sdata.fRatePatch[fPatch[i]];
@@ -1321,6 +1330,9 @@
 #ifdef HAVE_ROOT
         Camera *cam = (Camera*)fRatesCanv->GetCanvas()->FindObject("Camera");
-        for (int i=0; i<1440; i++)
-            cam->SetEnable(i, sdata.IsEnabled(i));
+        for (int isw=0; isw<1440; isw++)
+        {
+            const int ihw = fPixelMapHW[isw];
+            cam->SetEnable(isw, sdata.IsEnabled(ihw));
+        }
 
         fRatesCanv->GetCanvas()->Modified();
@@ -1328,11 +1340,17 @@
 #endif
 
-        fPixelEnable->setChecked(sdata.IsEnabled(fPixelIdx->value()));
-
-
-
-        const int patch1 = fThresholdIdx->value();
-        if (patch1>=0)
-            fThresholdVal->setValue(sdata.fThreshold[patch1]);
+        {
+            const int  isw = fPixelIdx->value();
+            const int  ihw = fPixelMapHW[isw];
+            const bool on  = sdata.IsEnabled(ihw);
+            fPixelEnable->setChecked(on);
+        }
+
+        if (fThresholdIdx->value()>=0)
+        {
+            const int isw = fThresholdIdx->value();
+            const int ihw = fPatchMapHW[isw];
+            fThresholdVal->setValue(sdata.fThreshold[ihw]);
+        }
 
         fPrescalingVal->setValue(sdata.fPrescaling[0]);
@@ -1837,11 +1855,8 @@
 
                 Camera *cam = static_cast<Camera*>(obj);
-                const int idx = cam->GetIdx(xx, yy);
-
-                cout << "Select: " << idx << endl;
-
-                fPixelIdx->setValue(idx);
-                ChoosePixel(*cam, idx);
-
+                const int isw = cam->GetIdx(xx, yy);
+
+                fPixelIdx->setValue(isw);
+                ChoosePixel(*cam, isw);
             }
             return;
@@ -1856,13 +1871,13 @@
 
                 Camera *cam = static_cast<Camera*>(obj);
-                const int idx = cam->GetIdx(xx, yy);
-
-                cout << "Toggle: " << idx << endl;
-
-                ChoosePixel(*cam, idx);
-
-                fPixelIdx->setValue(idx);
-
-                Dim::SendCommand("FTM_CONTROL/TOGGLE_PIXEL", uint16_t(idx));
+                const int isw = cam->GetIdx(xx, yy);
+
+                ChoosePixel(*cam, isw);
+
+                fPixelIdx->setValue(isw);
+
+                const uint16_t ihw = fPixelMapHW[isw];
+
+                Dim::SendCommand("FTM_CONTROL/TOGGLE_PIXEL", ihw);
             }
 
@@ -1899,8 +1914,18 @@
 
             Camera *cam = static_cast<Camera*>(obj);
-            int idx = fPatch[cam->GetIdx(xx, yy)];
-
-            tipText+="  Patch=";
-            tipText+=QString::number(idx);
+
+            const int isw = cam->GetIdx(xx, yy);
+            const int ihw = fPixelMapHW[isw];
+
+            const int idx = fPatch[isw];
+
+            const int patch =  ihw%4;
+            const int board = (ihw/4)%10;
+            const int crate = (ihw/4)/10;
+
+            ostringstream str;
+            str << "  HW=" << ihw << "  Patch=" << idx << "  (Crate=" << crate << " Board=" << board << " Patch=" << patch << ")";
+
+            tipText += str.str().c_str();
         }
 
@@ -1918,13 +1943,25 @@
     }
 
-    void ChoosePatch(Camera &cam, int idx)
+    void ChoosePatch(Camera &cam, int isw)
     {
         cam.Reset();
 
-        fThresholdIdx->setValue(idx);
-
-        const int patch = idx%4;
-        const int board = (idx/4)%10;
-        const int crate = (idx/4)/10;
+        fThresholdIdx->setValue(isw);
+
+        const int ihw = isw<0 ? 0 : fPatchMapHW[isw];
+
+        fPatchRate->setEnabled(isw>=0);
+        fThresholdCrate->setEnabled(isw>=0);
+        fThresholdBoard->setEnabled(isw>=0);
+        fThresholdPatch->setEnabled(isw>=0);
+
+        if (isw<0)
+            return;
+
+        const int patch = ihw%4;
+        const int board = (ihw/4)%10;
+        const int crate = (ihw/4)/10;
+
+        fInChoosePatch = true;
 
         fThresholdCrate->setValue(crate);
@@ -1932,46 +1969,55 @@
         fThresholdPatch->setValue(patch);
 
-        //fThresholdVal->setEnabled(idx>=0);
-        //fThresholdVolt->setEnabled(idx>=0);
-        fPatchRate->setEnabled(idx>=0);
-        if (idx<0)
-            return;
-
-        fThresholdVal->setValue(fFtmStaticData.fThreshold[idx]);
-        fPatchRate->setValue(cam.GetData(idx));
+        fInChoosePatch = false;
+
+        fThresholdVal->setValue(fFtmStaticData.fThreshold[ihw]);
+        fPatchRate->setValue(cam.GetData(isw));
 
         for (unsigned int i=0; i<fPatch.size(); i++)
-            if (fPatch[i]==idx)
+            if (fPatch[i]==isw)
                 cam.SetBold(i);
     }
 
-    void ChoosePixel(Camera &cam, int idx)
-    {
-        cam.SetWhite(idx);
-        ChoosePatch(cam, fPatch[idx]);
-
-        fPixelEnable->setChecked(fFtmStaticData.IsEnabled(idx));
-    }
-
-    void UpdatePatch(int idx)
+    void ChoosePixel(Camera &cam, int isw)
+    {
+        cam.SetWhite(isw);
+        ChoosePatch(cam, fPatch[isw]);
+
+        const int  ihw = fPixelMapHW[isw];
+        const bool on  = fFtmStaticData.IsEnabled(ihw);
+        fPixelEnable->setChecked(on);
+    }
+
+    void UpdatePatch(int isw)
     {
         Camera *cam = (Camera*)fRatesCanv->GetCanvas()->FindObject("Camera");
-        ChoosePatch(*cam, idx);
-    }
-
-    void on_fThresholdIdx_valueChanged(int idx)
-    {
-        UpdatePatch(idx);
+        ChoosePatch(*cam, isw);
+    }
+
+    void on_fThresholdIdx_valueChanged(int isw)
+    {
+        UpdatePatch(isw);
+
+        fRatesCanv->GetCanvas()->Modified();
+        fRatesCanv->GetCanvas()->Update();
     }
 
     void UpdateThresholdIdx()
     {
+        if (fInChoosePatch)
+            return;
+
         const int crate = fThresholdCrate->value();
         const int board = fThresholdBoard->value();
         const int patch = fThresholdPatch->value();
 
-        const int id = patch + board*4 + crate*40;
-
-        UpdatePatch(id);
+        const int ihw = patch + board*4 + crate*40;
+
+        int isw = 0;
+        for (; isw<160; isw++)
+            if (ihw==fPatchMapHW[isw])
+                break;
+
+        UpdatePatch(isw);
     }
 
@@ -1989,8 +2035,8 @@
     }
 
-    void on_fPixelIdx_valueChanged(int idx)
+    void on_fPixelIdx_valueChanged(int isw)
     {
         Camera *cam = (Camera*)fRatesCanv->GetCanvas()->FindObject("Camera");
-        ChoosePixel(*cam, idx);
+        ChoosePixel(*cam, isw);
 
         fRatesCanv->GetCanvas()->Modified();
@@ -1998,4 +2044,38 @@
     }
 #endif
+
+    void on_fPixelEnable_stateChanged(int b)
+    {
+        if (fInHandler)
+            return;
+
+        const uint16_t isw = fPixelIdx->value();
+        const uint16_t ihw = fPixelMapHW[isw];
+
+        Dim::SendCommand(b==Qt::Unchecked ?
+                         "FTM_CONTROL/DISABLE_PIXEL" : "FTM_CONTROL/ENABLE_PIXEL",
+                         ihw);
+    }
+
+    void on_fPixelDisableOthers_clicked()
+    {
+        const uint16_t isw = fPixelIdx->value();
+        const uint16_t ihw = fPixelMapHW[isw];
+
+        Dim::SendCommand("FTM_CONTROL/DISABLE_ALL_PIXELS_EXCEPT", ihw);
+    }
+
+    void on_fThresholdVal_valueChanged(int v)
+    {
+        fThresholdVolt->setValue(2500./4095*v);
+
+        const int32_t isw = fThresholdIdx->value();
+        const int32_t ihw = fPatchMapHW[isw];
+
+        const int32_t d[2] = { ihw, v };
+
+        if (!fInHandler)
+            Dim::SendCommand("FTM_CONTROL/SET_THRESHOLD", d);
+    }
 
     TGraph fGraphFtmTemp[4];
@@ -2033,5 +2113,6 @@
 public:
     FactGui() :
-        fFtuStatus(40), //fMapHW(1440)
+        fFtuStatus(40), fPixelMapHW(1440), fPatchMapHW(160),
+        fInChoosePatch(false),
         fDimDNS("DIS_DNS/VERSION_NUMBER", 1, int(0), this),
 
@@ -2091,6 +2172,6 @@
 
         // --------------------------------------------------------------------------
-/*
-        ifstream fin2("FACTmapV3.txt");
+
+        ifstream fin2("MasterList-v3.txt");
 
         l = 0;
@@ -2102,5 +2183,30 @@
                 continue;
 
-            int softid, hardid, geom_i, geom_j,  gapd;
+            int softid, hardid, dummy;
+
+            stringstream str(buf);
+
+            str >> softid;
+            str >> dummy;
+            str >> hardid;
+
+            fPixelMapHW[softid] = hardid;
+
+            l++;
+        }
+
+        // --------------------------------------------------------------------------
+
+        ifstream fin3("PatchList.txt");
+
+        l = 0;
+
+        while (getline(fin3, buf, '\n'))
+        {
+            buf = Tools::Trim(buf);
+            if (buf[0]=='#')
+                continue;
+
+            int softid, hardid;
 
             stringstream str(buf);
@@ -2108,14 +2214,10 @@
             str >> softid;
             str >> hardid;
-            str >> geom_i;
-            str >> geom_j;
-            str >> gapd;
-
-            fMapHW[softid] = hardid;
-            fMapSW[hardid] = softid;
+
+            fPatchMapHW[softid] = hardid-1;
 
             l++;
         }
-*/
+
         // --------------------------------------------------------------------------
 #ifdef HAVE_ROOT
@@ -2201,5 +2303,5 @@
         cam->Draw();
 
-        ChoosePixel(*cam, 1);
+        ChoosePixel(*cam, 0);
 
 //        QTimer::singleShot(0, this, SLOT(slot_RootUpdate()));
Index: /trunk/FACT++/gui/MainWindow.cc
===================================================================
--- /trunk/FACT++/gui/MainWindow.cc	(revision 10958)
+++ /trunk/FACT++/gui/MainWindow.cc	(revision 10959)
@@ -205,14 +205,4 @@
 }
 
-void MainWindow::on_fThresholdVal_valueChanged(int v)
-{
-    fThresholdVolt->setValue(2500./4095*v);
-
-    const int32_t d[2] = { fThresholdIdx->value(), v };
-
-    if (!fInHandler)
-        Dim::SendCommand("FTM_CONTROL/SET_THRESHOLD", d);
-}
-
 void MainWindow::on_fTriggerInterval_valueChanged(int val)
 {
@@ -245,16 +235,12 @@
 }
 
-void MainWindow::on_fPixelEnable_stateChanged(int b)
-{
-    if (!fInHandler)
-        Dim::SendCommand(b==Qt::Unchecked ?
-                         "FTM_CONTROL/DISABLE_PIXEL" : "FTM_CONTROL/ENABLE_PIXEL",
-                         uint16_t(fPixelIdx->value()));
-}
-
-void MainWindow::on_fPixelDisableOthers_clicked()
-{
-    Dim::SendCommand("FTM_CONTROL/DISABLE_ALL_PIXELS_EXCEPT",
-                     uint16_t(fPixelIdx->value()));
+void MainWindow::on_fPixelEnableAll_clicked()
+{
+    Dim::SendCommand("FTM_CONTROL/ENABLE_PIXEL", int16_t(-1));
+}
+
+void MainWindow::on_fPixelDisableAll_clicked()
+{
+    Dim::SendCommand("FTM_CONTROL/DISABLE_PIXEL", int16_t(-1));
 }
 
Index: /trunk/FACT++/gui/design.ui
===================================================================
--- /trunk/FACT++/gui/design.ui	(revision 10958)
+++ /trunk/FACT++/gui/design.ui	(revision 10959)
@@ -2491,24 +2491,29 @@
               </item>
               <item>
+               <layout class="QHBoxLayout" name="horizontalLayout_24">
+                <property name="topMargin">
+                 <number>0</number>
+                </property>
+                <item>
+                 <widget class="QPushButton" name="fPixelEnableAll">
+                  <property name="text">
+                   <string>Enable all</string>
+                  </property>
+                 </widget>
+                </item>
+                <item>
+                 <widget class="QPushButton" name="fPixelDisableAll">
+                  <property name="text">
+                   <string>Disable all</string>
+                  </property>
+                 </widget>
+                </item>
+               </layout>
+              </item>
+              <item>
                <layout class="QHBoxLayout" name="horizontalLayout_22">
                 <property name="topMargin">
                  <number>0</number>
                 </property>
-                <item>
-                 <spacer name="horizontalSpacer_29">
-                  <property name="orientation">
-                   <enum>Qt::Horizontal</enum>
-                  </property>
-                  <property name="sizeType">
-                   <enum>QSizePolicy::Minimum</enum>
-                  </property>
-                  <property name="sizeHint" stdset="0">
-                   <size>
-                    <width>40</width>
-                    <height>20</height>
-                   </size>
-                  </property>
-                 </spacer>
-                </item>
                 <item>
                  <widget class="QPushButton" name="fPixelDisableOthers">
@@ -2558,5 +2563,5 @@
                 </property>
                 <property name="maximum">
-                 <number>1438</number>
+                 <number>159</number>
                 </property>
                 <property name="value">
@@ -2570,20 +2575,4 @@
                  <number>0</number>
                 </property>
-                <item>
-                 <spacer name="horizontalSpacer_31">
-                  <property name="orientation">
-                   <enum>Qt::Horizontal</enum>
-                  </property>
-                  <property name="sizeType">
-                   <enum>QSizePolicy::Minimum</enum>
-                  </property>
-                  <property name="sizeHint" stdset="0">
-                   <size>
-                    <width>40</width>
-                    <height>20</height>
-                   </size>
-                  </property>
-                 </spacer>
-                </item>
                 <item>
                  <widget class="QPushButton" name="fThresholdDisableOthers">
