Improved copying from Edit Data Grid - rough patch - Mailing list pgadmin-hackers

From Edward Di Geronimo Jr.
Subject Improved copying from Edit Data Grid - rough patch
Date
Msg-id 43E30729.3040902@xtracards.com
Whole thread Raw
List pgadmin-hackers
Hello,

I want to get involved in helping polish the pgAdmin interface. As a
first step, I've modified the copy for the Edit Data grid to have much
more flexiblity in copying data from the grid. The present code only
allows copying entire rows. With my changes, you can now copy rows,
columns, or the highlighted range.

The attached patch is very rough. The most notable rough spot is it
creates two new overloaded versions of sqlTable::GetExportLine which
duplicate a lot of code from the original. These can definitely be
consolidated, however, I wasn't sure what the preferred style would be
for it.

The next thing I would like to after finishing this is modify the query
windows to display the results in a grid instead of a list. The ability
to copy arbitrary sections of the results would be a huge help in my
daily work. I figured this could would most likely be reusable for that
goal.

Feedback would be appreciated.

Ed
Index: src/frm/frmEditGrid.cpp
===================================================================
--- src/frm/frmEditGrid.cpp    (revision 4983)
+++ src/frm/frmEditGrid.cpp    (working copy)
@@ -323,6 +323,7 @@
 }


+#if 0
 void frmEditGrid::OnCopy(wxCommandEvent &ev)
 {
     wxArrayInt rows=sqlGrid->GetSelectedRows();
@@ -345,8 +346,70 @@
     }
     SetStatusText(wxString::Format(_("%d rows copied to clipboard."), rows.GetCount()));
 }
+#endif

+void frmEditGrid::OnCopy(wxCommandEvent &ev)
+{
+    wxString str;
+    int copied = 0;
+    size_t i;

+    if (sqlGrid->GetSelectedRows().GetCount()) {
+        wxArrayInt rows=sqlGrid->GetSelectedRows();
+
+        for (i=0 ; i < rows.GetCount() ; i++)
+        {
+            str.Append(sqlGrid->GetTable()->GetExportLine(rows.Item(i)));
+
+            if (rows.GetCount() > 1)
+                str.Append(END_OF_LINE);
+        }
+
+        copied = rows.GetCount();
+    }
+    else if (sqlGrid->GetSelectedCols().GetCount()) {
+        wxArrayInt cols=sqlGrid->GetSelectedCols();
+        size_t numRows = sqlGrid->GetNumberRows();
+
+        for (i=0 ; i < numRows ; i++)
+        {
+            str.Append(sqlGrid->GetTable()->GetExportLine(i, cols));
+
+            if (numRows > 1)
+                str.Append(END_OF_LINE);
+        }
+
+        copied = numRows;
+    }
+    else if (sqlGrid->GetSelectionBlockTopLeft().GetCount() > 0 &&
+        sqlGrid->GetSelectionBlockBottomRight().GetCount() > 0) {
+        int x1, x2, y1, y2;
+
+        x1 = sqlGrid->GetSelectionBlockTopLeft()[0].GetCol();
+        x2 = sqlGrid->GetSelectionBlockBottomRight()[0].GetCol();
+        y1 = sqlGrid->GetSelectionBlockTopLeft()[0].GetRow();
+        y2 = sqlGrid->GetSelectionBlockBottomRight()[0].GetRow();
+
+        for (i = y1; i <= y2; i++) {
+            str.Append(sqlGrid->GetTable()->GetExportLine(i, x1, x2));
+
+            if (y2 > y1)
+                str.Append(END_OF_LINE);
+        }
+
+        copied = y2 - y1 + 1;
+    }
+
+    if (copied && wxTheClipboard->Open())
+    {
+        wxTheClipboard->SetData(new wxTextDataObject(str));
+        wxTheClipboard->Close();
+    }
+
+    SetStatusText(wxString::Format(_("%d rows copied to clipboard."), copied));
+}
+
+
 void frmEditGrid::OnHelp(wxCommandEvent &ev)
 {
     DisplayHelp(this, wxT("editgrid"), viewdata_xpm);
@@ -1439,6 +1502,79 @@
 }


+wxString sqlTable::GetExportLine(int row, int col1, int col2)
+{
+    wxString str;
+    cacheLine *line = GetLine(row);
+    int maxCol = (col2 < nCols - 1) ? col2 : (nCols - 1);
+    if (line)
+    {
+        int col;
+        for (col=col1 ; col <= maxCol ; col++)
+        {
+            if (col > col1)
+                str.Append(settings->GetExportColSeparator());
+            bool needQuote = settings->GetExportQuoting() > 1;
+
+            // find out if string
+            switch (columns[col].type)
+            {
+                case PGTYPCLASS_NUMERIC:
+                case PGTYPCLASS_BOOL:
+                    break;
+                default:
+                    needQuote=true;
+                    break;
+            }
+            if (needQuote)
+                str.Append(settings->GetExportQuoteChar());
+
+            str.Append(line->cols[col]);
+
+            if (needQuote)
+                str.Append(settings->GetExportQuoteChar());
+        }
+    }
+    return str;
+}
+
+
+wxString sqlTable::GetExportLine(int row, wxArrayInt cols)
+{
+    wxString str;
+    cacheLine *line = GetLine(row);
+    if (line)
+    {
+        int col;
+        for (col=0 ; col < cols.Count() ; col++)
+        {
+            if (col > 0)
+                str.Append(settings->GetExportColSeparator());
+            bool needQuote = settings->GetExportQuoting() > 1;
+
+            // find out if string
+            switch (columns[cols[col]].type)
+            {
+                case PGTYPCLASS_NUMERIC:
+                case PGTYPCLASS_BOOL:
+                    break;
+                default:
+                    needQuote=true;
+                    break;
+            }
+            if (needQuote)
+                str.Append(settings->GetExportQuoteChar());
+
+            str.Append(line->cols[cols[col]]);
+
+            if (needQuote)
+                str.Append(settings->GetExportQuoteChar());
+        }
+    }
+    return str;
+}
+
+
 wxString sqlTable::GetColLabelValue(int col)
 {
     wxString label=columns[col].name + wxT("\n");
Index: src/include/frmEditGrid.h
===================================================================
--- src/include/frmEditGrid.h    (revision 4983)
+++ src/include/frmEditGrid.h    (working copy)
@@ -113,6 +113,8 @@
     bool DeleteRows(size_t pos, size_t rows);
     int  LastRow() { return lastRow; }
     wxString GetExportLine(int row);
+    wxString GetExportLine(int row, int col1, int col2);
+    wxString sqlTable::GetExportLine(int row, wxArrayInt cols);

     bool CheckInCache(int row);


pgadmin-hackers by date:

Previous
From: "Dave Page"
Date:
Subject: Re: execute sql script w/o stopping after error?
Next
From: "Magnus Hagander"
Date:
Subject: Re: Improved copying from Edit Data Grid - rough patch